• Petr Mladek's avatar
    printk/console: Check consistent sequence number when handling race in console_unlock() · 11e4b63a
    Petr Mladek authored
    The standard printk() tries to flush the message to the console
    immediately. It tries to take the console lock. If the lock is
    already taken then the current owner is responsible for flushing
    even the new message.
    
    There is a small race window between checking whether a new message is
    available and releasing the console lock. It is solved by re-checking
    the state after releasing the console lock. If the check is positive
    then console_unlock() tries to take the lock again and process the new
    message as well.
    
    The commit 996e9666 ("printk: remove logbuf_lock") causes that
    console_seq is not longer read atomically. As a result, the re-check might
    be done with an inconsistent 64-bit index.
    
    Solve it by using the last sequence number that has been checked under
    the console lock. In the worst case, it will take the lock again only
    to realized that the new message has already been proceed. But it
    was possible even before.
    
    The variable next_seq is marked as __maybe_unused to call down compiler
    warning when CONFIG_PRINTK is not defined.
    
    Fixes: commit 996e9666 ("printk: remove logbuf_lock")
    Reported-by: kernel test robot <lkp@intel.com>  # unused next_seq warning
    Cc: stable@vger.kernel.org # 5.13
    Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
    Acked-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
    Reviewed-by: default avatarJohn Ogness <john.ogness@linutronix.de>
    Link: https://lore.kernel.org/r/20210702150657.26760-1-pmladek@suse.com
    11e4b63a
printk.c 92.8 KB