Commit 100bdef2 authored by John Ogness's avatar John Ogness Committed by Petr Mladek

console: introduce wrappers to read/write console flags

After switching to SRCU for console list iteration, some readers
will begin readings console->flags as a data race. Locklessly
reading console->flags provides a consistent value because there
is at most one CPU modifying console->flags and that CPU is
using only read-modify-write operations.

Introduce a wrapper for SRCU iterators to read console flags.
Introduce a matching wrapper to write to flags of registered
consoles. Writing to flags of registered consoles is synchronized
by the console_list_lock.
Signed-off-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20221116162152.193147-13-john.ogness@linutronix.de
parent 4dc64682
......@@ -183,6 +183,51 @@ extern void console_list_unlock(void) __releases(console_mutex);
extern struct hlist_head console_list;
/**
* console_srcu_read_flags - Locklessly read the console flags
* @con: struct console pointer of console to read flags from
*
* This function provides the necessary READ_ONCE() and data_race()
* notation for locklessly reading the console flags. The READ_ONCE()
* in this function matches the WRITE_ONCE() when @flags are modified
* for registered consoles with console_srcu_write_flags().
*
* Only use this function to read console flags when locklessly
* iterating the console list via srcu.
*
* Context: Any context.
*/
static inline short console_srcu_read_flags(const struct console *con)
{
WARN_ON_ONCE(!console_srcu_read_lock_is_held());
/*
* Locklessly reading console->flags provides a consistent
* read value because there is at most one CPU modifying
* console->flags and that CPU is using only read-modify-write
* operations to do so.
*/
return data_race(READ_ONCE(con->flags));
}
/**
* console_srcu_write_flags - Write flags for a registered console
* @con: struct console pointer of console to write flags to
* @flags: new flags value to write
*
* Only use this function to write flags for registered consoles. It
* requires holding the console_list_lock.
*
* Context: Any context.
*/
static inline void console_srcu_write_flags(struct console *con, short flags)
{
lockdep_assert_console_list_lock_held();
/* This matches the READ_ONCE() in console_srcu_read_flags(). */
WRITE_ONCE(con->flags, flags);
}
/**
* for_each_console_srcu() - Iterator over registered consoles
* @con: struct console pointer used as loop cursor
......
......@@ -3069,7 +3069,7 @@ void console_stop(struct console *console)
__pr_flush(console, 1000, true);
console_list_lock();
console_lock();
console->flags &= ~CON_ENABLED;
console_srcu_write_flags(console, console->flags & ~CON_ENABLED);
console_unlock();
console_list_unlock();
......@@ -3087,7 +3087,7 @@ void console_start(struct console *console)
{
console_list_lock();
console_lock();
console->flags |= CON_ENABLED;
console_srcu_write_flags(console, console->flags | CON_ENABLED);
console_unlock();
console_list_unlock();
__pr_flush(console, 1000, true);
......@@ -3343,7 +3343,7 @@ void register_console(struct console *newcon)
} else if (newcon->flags & CON_CONSDEV) {
/* Only the new head can have CON_CONSDEV set. */
console_first()->flags &= ~CON_CONSDEV;
console_srcu_write_flags(console_first(), console_first()->flags & ~CON_CONSDEV);
hlist_add_head_rcu(&newcon->node, &console_list);
} else {
......@@ -3400,7 +3400,7 @@ static int unregister_console_locked(struct console *console)
console_lock();
/* Disable it unconditionally */
console->flags &= ~CON_ENABLED;
console_srcu_write_flags(console, console->flags & ~CON_ENABLED);
if (hlist_unhashed(&console->node)) {
console_unlock();
......@@ -3419,7 +3419,7 @@ static int unregister_console_locked(struct console *console)
* console has any device attached. Oh well....
*/
if (!hlist_empty(&console_list) && console->flags & CON_CONSDEV)
console_first()->flags |= CON_CONSDEV;
console_srcu_write_flags(console_first(), console_first()->flags | CON_CONSDEV);
console_unlock();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment