Skip to content

Commit 1fd4224

Browse files
jognesspmladek
authored andcommitted
console: introduce console_is_registered()
Currently it is not possible for drivers to detect if they have already successfully registered their console. Several drivers have multiple paths that lead to console registration. To avoid attempting a 2nd registration (which leads to a WARN), drivers are implementing their own solution. Introduce console_is_registered() so drivers can easily identify if their console is currently registered. A _locked() variant is also provided if the caller is already holding the console_list_lock. Signed-off-by: John Ogness <john.ogness@linutronix.de> Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20221116162152.193147-22-john.ogness@linutronix.de
1 parent 8cb15f7 commit 1fd4224

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

include/linux/console.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,34 @@ static inline void console_srcu_write_flags(struct console *con, short flags)
228228
WRITE_ONCE(con->flags, flags);
229229
}
230230

231+
/* Variant of console_is_registered() when the console_list_lock is held. */
232+
static inline bool console_is_registered_locked(const struct console *con)
233+
{
234+
lockdep_assert_console_list_lock_held();
235+
return !hlist_unhashed(&con->node);
236+
}
237+
238+
/*
239+
* console_is_registered - Check if the console is registered
240+
* @con: struct console pointer of console to check
241+
*
242+
* Context: Process context. May sleep while acquiring console list lock.
243+
* Return: true if the console is in the console list, otherwise false.
244+
*
245+
* If false is returned for a console that was previously registered, it
246+
* can be assumed that the console's unregistration is fully completed,
247+
* including the exit() callback after console list removal.
248+
*/
249+
static inline bool console_is_registered(const struct console *con)
250+
{
251+
bool ret;
252+
253+
console_list_lock();
254+
ret = console_is_registered_locked(con);
255+
console_list_unlock();
256+
return ret;
257+
}
258+
231259
/**
232260
* for_each_console_srcu() - Iterator over registered consoles
233261
* @con: struct console pointer used as loop cursor

kernel/printk/printk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3443,7 +3443,7 @@ static int unregister_console_locked(struct console *console)
34433443
/* Disable it unconditionally */
34443444
console_srcu_write_flags(console, console->flags & ~CON_ENABLED);
34453445

3446-
if (hlist_unhashed(&console->node)) {
3446+
if (!console_is_registered_locked(console)) {
34473447
console_unlock();
34483448
return -ENODEV;
34493449
}

0 commit comments

Comments
 (0)