linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv2] printk: CON_PRINTBUFFER console registration is a bit racy
@ 2018-09-28  9:53 Sergey Senozhatsky
  2018-10-03  9:23 ` Petr Mladek
  0 siblings, 1 reply; 3+ messages in thread
From: Sergey Senozhatsky @ 2018-09-28  9:53 UTC (permalink / raw)
  To: Petr Mladek, Steven Rostedt
  Cc: Sergey Senozhatsky, linux-kernel, Sergey Senozhatsky

CON_PRINTBUFFER console registration requires us to do several
preparation steps:
- Rollback console_seq to replay logbuf messages which were already
  seen on other consoles;
- Set exclusive_console flag so console_unlock() will ->write() logbuf
  messages only to the exclusive_console driver.

The way we do it, however, is a bit racy

	logbuf_lock_irqsave(flags);
	console_seq = syslog_seq;
	console_idx = syslog_idx;
	logbuf_unlock_irqrestore(flags);
						<< preemption enabled
						<< irqs enabled
	exclusive_console = newcon;
	console_unlock();

We rollback console_seq under logbuf_lock with IRQs disabled, but
we set exclusive_console with local IRQs enabled and logbuf unlocked.
If the system oops-es or panic-s before we set exclusive_console - and
given that we have IRQs and preemption enabled there is such a
possibility - we will re-play all logbuf messages to every registered
console, which may be a bit annoying and time consuming.

Move exclusive_console assignment to the same IRQs-disabled and
logbuf_lock-protected section where we rollback console_seq.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---

v2: added comment (Petr)

 kernel/printk/printk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 308497194bd4..24a81ba9ec77 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2742,14 +2742,18 @@ void register_console(struct console *newcon)
 		logbuf_lock_irqsave(flags);
 		console_seq = syslog_seq;
 		console_idx = syslog_idx;
-		logbuf_unlock_irqrestore(flags);
 		/*
 		 * We're about to replay the log buffer.  Only do this to the
 		 * just-registered console to avoid excessive message spam to
 		 * the already-registered consoles.
+		 *
+		 * Set exclusive_console with disabled interrupts to reduce
+		 * race window with eventual console_flush_on_panic() that
+		 * ignores console_lock.
 		 */
 		exclusive_console = newcon;
 		exclusive_console_stop_seq = console_seq;
+		logbuf_unlock_irqrestore(flags);
 	}
 	console_unlock();
 	console_sysfs_notify();
-- 
2.19.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCHv2] printk: CON_PRINTBUFFER console registration is a bit racy
  2018-09-28  9:53 [PATCHv2] printk: CON_PRINTBUFFER console registration is a bit racy Sergey Senozhatsky
@ 2018-10-03  9:23 ` Petr Mladek
  2018-10-03 12:27   ` Sergey Senozhatsky
  0 siblings, 1 reply; 3+ messages in thread
From: Petr Mladek @ 2018-10-03  9:23 UTC (permalink / raw)
  To: Sergey Senozhatsky; +Cc: Steven Rostedt, linux-kernel, Sergey Senozhatsky

On Fri 2018-09-28 18:53:04, Sergey Senozhatsky wrote:
> CON_PRINTBUFFER console registration requires us to do several
> preparation steps:
> - Rollback console_seq to replay logbuf messages which were already
>   seen on other consoles;
> - Set exclusive_console flag so console_unlock() will ->write() logbuf
>   messages only to the exclusive_console driver.
> 
> The way we do it, however, is a bit racy
> 
> 	logbuf_lock_irqsave(flags);
> 	console_seq = syslog_seq;
> 	console_idx = syslog_idx;
> 	logbuf_unlock_irqrestore(flags);
> 						<< preemption enabled
> 						<< irqs enabled
> 	exclusive_console = newcon;
> 	console_unlock();
> 
> We rollback console_seq under logbuf_lock with IRQs disabled, but
> we set exclusive_console with local IRQs enabled and logbuf unlocked.
> If the system oops-es or panic-s before we set exclusive_console - and
> given that we have IRQs and preemption enabled there is such a
> possibility - we will re-play all logbuf messages to every registered
> console, which may be a bit annoying and time consuming.
> 
> Move exclusive_console assignment to the same IRQs-disabled and
> logbuf_lock-protected section where we rollback console_seq.
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>

JFYI, I have just pushed this into printk.git, for-4.20 branch.

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCHv2] printk: CON_PRINTBUFFER console registration is a bit racy
  2018-10-03  9:23 ` Petr Mladek
@ 2018-10-03 12:27   ` Sergey Senozhatsky
  0 siblings, 0 replies; 3+ messages in thread
From: Sergey Senozhatsky @ 2018-10-03 12:27 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Sergey Senozhatsky, Steven Rostedt, linux-kernel, Sergey Senozhatsky

On (10/03/18 11:23), Petr Mladek wrote:
> On Fri 2018-09-28 18:53:04, Sergey Senozhatsky wrote:
> > CON_PRINTBUFFER console registration requires us to do several
> > preparation steps:
> > - Rollback console_seq to replay logbuf messages which were already
> >   seen on other consoles;
> > - Set exclusive_console flag so console_unlock() will ->write() logbuf
> >   messages only to the exclusive_console driver.
> > 
> > The way we do it, however, is a bit racy
> > 
> > 	logbuf_lock_irqsave(flags);
> > 	console_seq = syslog_seq;
> > 	console_idx = syslog_idx;
> > 	logbuf_unlock_irqrestore(flags);
> > 						<< preemption enabled
> > 						<< irqs enabled
> > 	exclusive_console = newcon;
> > 	console_unlock();
> > 
> > We rollback console_seq under logbuf_lock with IRQs disabled, but
> > we set exclusive_console with local IRQs enabled and logbuf unlocked.
> > If the system oops-es or panic-s before we set exclusive_console - and
> > given that we have IRQs and preemption enabled there is such a
> > possibility - we will re-play all logbuf messages to every registered
> > console, which may be a bit annoying and time consuming.
> > 
> > Move exclusive_console assignment to the same IRQs-disabled and
> > logbuf_lock-protected section where we rollback console_seq.
> > 
> > Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> 
> JFYI, I have just pushed this into printk.git, for-4.20 branch.

Thanks.

	-ss

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-10-03 12:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-28  9:53 [PATCHv2] printk: CON_PRINTBUFFER console registration is a bit racy Sergey Senozhatsky
2018-10-03  9:23 ` Petr Mladek
2018-10-03 12:27   ` Sergey Senozhatsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).