|
Petr Mladek |
f003e8 |
From 11e4b63abbe23872b45f325a7c6c8b7f9ff42cad Mon Sep 17 00:00:00 2001
|
|
Petr Mladek |
f003e8 |
From: Petr Mladek <pmladek@suse.com>
|
|
Petr Mladek |
f003e8 |
Date: Fri, 2 Jul 2021 17:06:57 +0200
|
|
Petr Mladek |
f003e8 |
Subject: [PATCH] printk/console: Check consistent sequence number when
|
|
Petr Mladek |
f003e8 |
handling race in console_unlock()
|
|
Petr Mladek |
f003e8 |
Git-commit: 11e4b63abbe23872b45f325a7c6c8b7f9ff42cad
|
|
Petr Mladek |
f003e8 |
Patch-mainline: 5.15-rc1
|
|
Takashi Iwai |
4fca6a |
References: bsc#1190111 stable-5.14.6
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
The standard printk() tries to flush the message to the console
|
|
Petr Mladek |
f003e8 |
immediately. It tries to take the console lock. If the lock is
|
|
Petr Mladek |
f003e8 |
already taken then the current owner is responsible for flushing
|
|
Petr Mladek |
f003e8 |
even the new message.
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
There is a small race window between checking whether a new message is
|
|
Petr Mladek |
f003e8 |
available and releasing the console lock. It is solved by re-checking
|
|
Petr Mladek |
f003e8 |
the state after releasing the console lock. If the check is positive
|
|
Petr Mladek |
f003e8 |
then console_unlock() tries to take the lock again and process the new
|
|
Petr Mladek |
f003e8 |
message as well.
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
The commit 996e966640ddea7b535c ("printk: remove logbuf_lock") causes that
|
|
Petr Mladek |
f003e8 |
console_seq is not longer read atomically. As a result, the re-check might
|
|
Petr Mladek |
f003e8 |
be done with an inconsistent 64-bit index.
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
Solve it by using the last sequence number that has been checked under
|
|
Petr Mladek |
f003e8 |
the console lock. In the worst case, it will take the lock again only
|
|
Petr Mladek |
f003e8 |
to realized that the new message has already been proceed. But it
|
|
Petr Mladek |
f003e8 |
was possible even before.
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
The variable next_seq is marked as __maybe_unused to call down compiler
|
|
Petr Mladek |
f003e8 |
warning when CONFIG_PRINTK is not defined.
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
Fixes: commit 996e966640ddea7b535c ("printk: remove logbuf_lock")
|
|
Petr Mladek |
f003e8 |
Reported-by: kernel test robot <lkp@intel.com> # unused next_seq warning
|
|
Petr Mladek |
f003e8 |
Cc: stable@vger.kernel.org # 5.13
|
|
Petr Mladek |
f003e8 |
Signed-off-by: Petr Mladek <pmladek@suse.com>
|
|
Petr Mladek |
f003e8 |
Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org>
|
|
Petr Mladek |
f003e8 |
Reviewed-by: John Ogness <john.ogness@linutronix.de>
|
|
Petr Mladek |
f003e8 |
Link: https://lore.kernel.org/r/20210702150657.26760-1-pmladek@suse.com
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
---
|
|
Petr Mladek |
f003e8 |
kernel/printk/printk.c | 7 +++++--
|
|
Petr Mladek |
f003e8 |
1 file changed, 5 insertions(+), 2 deletions(-)
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
|
|
Petr Mladek |
f003e8 |
index 142a58d124d9..6dad7da8f383 100644
|
|
Petr Mladek |
f003e8 |
--- a/kernel/printk/printk.c
|
|
Petr Mladek |
f003e8 |
+++ b/kernel/printk/printk.c
|
|
Petr Mladek |
f003e8 |
@@ -2545,6 +2545,7 @@ void console_unlock(void)
|
|
Petr Mladek |
f003e8 |
bool do_cond_resched, retry;
|
|
Petr Mladek |
f003e8 |
struct printk_info info;
|
|
Petr Mladek |
f003e8 |
struct printk_record r;
|
|
Petr Mladek |
f003e8 |
+ u64 __maybe_unused next_seq;
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
if (console_suspended) {
|
|
Petr Mladek |
f003e8 |
up_console_sem();
|
|
Petr Mladek |
f003e8 |
@@ -2654,8 +2655,10 @@ void console_unlock(void)
|
|
Petr Mladek |
f003e8 |
cond_resched();
|
|
Petr Mladek |
f003e8 |
}
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
- console_locked = 0;
|
|
Petr Mladek |
f003e8 |
+ /* Get consistent value of the next-to-be-used sequence number. */
|
|
Petr Mladek |
f003e8 |
+ next_seq = console_seq;
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
+ console_locked = 0;
|
|
Petr Mladek |
f003e8 |
up_console_sem();
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
/*
|
|
Petr Mladek |
f003e8 |
@@ -2664,7 +2667,7 @@ void console_unlock(void)
|
|
Petr Mladek |
f003e8 |
* there's a new owner and the console_unlock() from them will do the
|
|
Petr Mladek |
f003e8 |
* flush, no worries.
|
|
Petr Mladek |
f003e8 |
*/
|
|
Petr Mladek |
f003e8 |
- retry = prb_read_valid(prb, console_seq, NULL);
|
|
Petr Mladek |
f003e8 |
+ retry = prb_read_valid(prb, next_seq, NULL);
|
|
Petr Mladek |
f003e8 |
printk_safe_exit_irqrestore(flags);
|
|
Petr Mladek |
f003e8 |
|
|
Petr Mladek |
f003e8 |
if (retry && console_trylock())
|
|
Petr Mladek |
f003e8 |
--
|
|
Petr Mladek |
f003e8 |
2.26.2
|
|
Petr Mladek |
f003e8 |
|