Commit d9a4af56 authored by Thomas Gleixner's avatar Thomas Gleixner Committed by Petr Mladek

printk: Convert console_drivers list to hlist

Replace the open coded single linked list with a hlist so a conversion
to SRCU protected list walks can reuse the existing primitives.
Co-developed-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Signed-off-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
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-3-john.ogness@linutronix.de
parent 9e409c47
...@@ -74,8 +74,9 @@ static void *c_start(struct seq_file *m, loff_t *pos) ...@@ -74,8 +74,9 @@ static void *c_start(struct seq_file *m, loff_t *pos)
static void *c_next(struct seq_file *m, void *v, loff_t *pos) static void *c_next(struct seq_file *m, void *v, loff_t *pos)
{ {
struct console *con = v; struct console *con = v;
++*pos; ++*pos;
return con->next; return hlist_entry_safe(con->node.next, struct console, node);
} }
static void c_stop(struct seq_file *m, void *v) static void c_stop(struct seq_file *m, void *v)
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#define _LINUX_CONSOLE_H_ 1 #define _LINUX_CONSOLE_H_ 1
#include <linux/atomic.h> #include <linux/atomic.h>
#include <linux/list.h>
#include <linux/types.h> #include <linux/types.h>
struct vc_data; struct vc_data;
...@@ -154,14 +155,16 @@ struct console { ...@@ -154,14 +155,16 @@ struct console {
u64 seq; u64 seq;
unsigned long dropped; unsigned long dropped;
void *data; void *data;
struct console *next; struct hlist_node node;
}; };
extern struct hlist_head console_list;
/* /*
* for_each_console() allows you to iterate on each console * for_each_console() allows you to iterate on each console
*/ */
#define for_each_console(con) \ #define for_each_console(con) \
for (con = console_drivers; con != NULL; con = con->next) hlist_for_each_entry(con, &console_list, node)
extern int console_set_on_cmdline; extern int console_set_on_cmdline;
extern struct console *early_console; extern struct console *early_console;
...@@ -174,7 +177,6 @@ enum con_flush_mode { ...@@ -174,7 +177,6 @@ enum con_flush_mode {
extern int add_preferred_console(char *name, int idx, char *options); extern int add_preferred_console(char *name, int idx, char *options);
extern void register_console(struct console *); extern void register_console(struct console *);
extern int unregister_console(struct console *); extern int unregister_console(struct console *);
extern struct console *console_drivers;
extern void console_lock(void); extern void console_lock(void);
extern int console_trylock(void); extern int console_trylock(void);
extern void console_unlock(void); extern void console_unlock(void);
......
...@@ -79,13 +79,12 @@ int oops_in_progress; ...@@ -79,13 +79,12 @@ int oops_in_progress;
EXPORT_SYMBOL(oops_in_progress); EXPORT_SYMBOL(oops_in_progress);
/* /*
* console_sem protects the console_drivers list, and also * console_sem protects console_list and console->flags updates, and also
* provides serialisation for access to the entire console * provides serialization for access to the entire console driver system.
* driver system.
*/ */
static DEFINE_SEMAPHORE(console_sem); static DEFINE_SEMAPHORE(console_sem);
struct console *console_drivers; HLIST_HEAD(console_list);
EXPORT_SYMBOL_GPL(console_drivers); EXPORT_SYMBOL_GPL(console_list);
/* /*
* System may need to suppress printk message under certain * System may need to suppress printk message under certain
...@@ -2556,7 +2555,7 @@ static int console_cpu_notify(unsigned int cpu) ...@@ -2556,7 +2555,7 @@ static int console_cpu_notify(unsigned int cpu)
* console_lock - lock the console system for exclusive use. * console_lock - lock the console system for exclusive use.
* *
* Acquires a lock which guarantees that the caller has * Acquires a lock which guarantees that the caller has
* exclusive access to the console system and the console_drivers list. * exclusive access to the console system and console_list.
* *
* Can sleep, returns nothing. * Can sleep, returns nothing.
*/ */
...@@ -2576,7 +2575,7 @@ EXPORT_SYMBOL(console_lock); ...@@ -2576,7 +2575,7 @@ EXPORT_SYMBOL(console_lock);
* console_trylock - try to lock the console system for exclusive use. * console_trylock - try to lock the console system for exclusive use.
* *
* Try to acquire a lock which guarantees that the caller has exclusive * Try to acquire a lock which guarantees that the caller has exclusive
* access to the console system and the console_drivers list. * access to the console system and console_list.
* *
* returns 1 on success, and 0 on failure to acquire the lock. * returns 1 on success, and 0 on failure to acquire the lock.
*/ */
...@@ -2940,11 +2939,20 @@ void console_flush_on_panic(enum con_flush_mode mode) ...@@ -2940,11 +2939,20 @@ void console_flush_on_panic(enum con_flush_mode mode)
console_may_schedule = 0; console_may_schedule = 0;
if (mode == CONSOLE_REPLAY_ALL) { if (mode == CONSOLE_REPLAY_ALL) {
struct hlist_node *tmp;
struct console *c; struct console *c;
u64 seq; u64 seq;
seq = prb_first_valid_seq(prb); seq = prb_first_valid_seq(prb);
for_each_console(c) /*
* This cannot use for_each_console() because it's not established
* that the current context has console locked and neither there is
* a guarantee that there is no concurrency in that case.
*
* Open code it for documentation purposes and pretend that
* it works.
*/
hlist_for_each_entry_safe(c, tmp, &console_list, node)
c->seq = seq; c->seq = seq;
} }
console_unlock(); console_unlock();
...@@ -3081,6 +3089,9 @@ static void try_enable_default_console(struct console *newcon) ...@@ -3081,6 +3089,9 @@ static void try_enable_default_console(struct console *newcon)
(con->flags & CON_BOOT) ? "boot" : "", \ (con->flags & CON_BOOT) ? "boot" : "", \
con->name, con->index, ##__VA_ARGS__) con->name, con->index, ##__VA_ARGS__)
#define console_first() \
hlist_entry(console_list.first, struct console, node)
/* /*
* The console driver calls this routine during kernel initialization * The console driver calls this routine during kernel initialization
* to register the console printing procedure with printk() and to * to register the console printing procedure with printk() and to
...@@ -3140,8 +3151,8 @@ void register_console(struct console *newcon) ...@@ -3140,8 +3151,8 @@ void register_console(struct console *newcon)
* flag set and will be first in the list. * flag set and will be first in the list.
*/ */
if (preferred_console < 0) { if (preferred_console < 0) {
if (!console_drivers || !console_drivers->device || if (hlist_empty(&console_list) || !console_first()->device ||
console_drivers->flags & CON_BOOT) { console_first()->flags & CON_BOOT) {
try_enable_default_console(newcon); try_enable_default_console(newcon);
} }
} }
...@@ -3169,20 +3180,22 @@ void register_console(struct console *newcon) ...@@ -3169,20 +3180,22 @@ void register_console(struct console *newcon)
} }
/* /*
* Put this console in the list - keep the * Put this console in the list - keep the
* preferred driver at the head of the list. * preferred driver at the head of the list.
*/ */
console_lock(); console_lock();
if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { if (hlist_empty(&console_list)) {
newcon->next = console_drivers; /* Ensure CON_CONSDEV is always set for the head. */
console_drivers = newcon;
if (newcon->next)
newcon->next->flags &= ~CON_CONSDEV;
/* Ensure this flag is always set for the head of the list */
newcon->flags |= CON_CONSDEV; newcon->flags |= CON_CONSDEV;
hlist_add_head(&newcon->node, &console_list);
} else if (newcon->flags & CON_CONSDEV) {
/* Only the new head can have CON_CONSDEV set. */
console_first()->flags &= ~CON_CONSDEV;
hlist_add_head(&newcon->node, &console_list);
} else { } else {
newcon->next = console_drivers->next; hlist_add_behind(&newcon->node, console_list.first);
console_drivers->next = newcon;
} }
newcon->dropped = 0; newcon->dropped = 0;
...@@ -3209,16 +3222,18 @@ void register_console(struct console *newcon) ...@@ -3209,16 +3222,18 @@ void register_console(struct console *newcon)
if (bootcon_enabled && if (bootcon_enabled &&
((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
!keep_bootcon) { !keep_bootcon) {
for_each_console(con) struct hlist_node *tmp;
hlist_for_each_entry_safe(con, tmp, &console_list, node) {
if (con->flags & CON_BOOT) if (con->flags & CON_BOOT)
unregister_console(con); unregister_console(con);
}
} }
} }
EXPORT_SYMBOL(register_console); EXPORT_SYMBOL(register_console);
int unregister_console(struct console *console) int unregister_console(struct console *console)
{ {
struct console *con;
int res; int res;
con_printk(KERN_INFO, console, "disabled\n"); con_printk(KERN_INFO, console, "disabled\n");
...@@ -3229,32 +3244,30 @@ int unregister_console(struct console *console) ...@@ -3229,32 +3244,30 @@ int unregister_console(struct console *console)
if (res > 0) if (res > 0)
return 0; return 0;
res = -ENODEV;
console_lock(); console_lock();
if (console_drivers == console) {
console_drivers=console->next; /* Disable it unconditionally */
res = 0; console->flags &= ~CON_ENABLED;
} else {
for_each_console(con) { if (hlist_unhashed(&console->node)) {
if (con->next == console) { console_unlock();
con->next = console->next; return -ENODEV;
res = 0;
break;
}
}
} }
if (res) hlist_del_init(&console->node);
goto out_disable_unlock;
/* /*
* <HISTORICAL>
* If this isn't the last console and it has CON_CONSDEV set, we * If this isn't the last console and it has CON_CONSDEV set, we
* need to set it on the next preferred console. * need to set it on the next preferred console.
* </HISTORICAL>
*
* The above makes no sense as there is no guarantee that the next
* console has any device attached. Oh well....
*/ */
if (console_drivers != NULL && console->flags & CON_CONSDEV) if (!hlist_empty(&console_list) && console->flags & CON_CONSDEV)
console_drivers->flags |= CON_CONSDEV; console_first()->flags |= CON_CONSDEV;
console->flags &= ~CON_ENABLED;
console_unlock(); console_unlock();
console_sysfs_notify(); console_sysfs_notify();
...@@ -3262,12 +3275,6 @@ int unregister_console(struct console *console) ...@@ -3262,12 +3275,6 @@ int unregister_console(struct console *console)
res = console->exit(console); res = console->exit(console);
return res; return res;
out_disable_unlock:
console->flags &= ~CON_ENABLED;
console_unlock();
return res;
} }
EXPORT_SYMBOL(unregister_console); EXPORT_SYMBOL(unregister_console);
...@@ -3317,10 +3324,11 @@ void __init console_init(void) ...@@ -3317,10 +3324,11 @@ void __init console_init(void)
*/ */
static int __init printk_late_init(void) static int __init printk_late_init(void)
{ {
struct hlist_node *tmp;
struct console *con; struct console *con;
int ret; int ret;
for_each_console(con) { hlist_for_each_entry_safe(con, tmp, &console_list, node) {
if (!(con->flags & CON_BOOT)) if (!(con->flags & CON_BOOT))
continue; continue;
...@@ -3340,6 +3348,7 @@ static int __init printk_late_init(void) ...@@ -3340,6 +3348,7 @@ static int __init printk_late_init(void)
unregister_console(con); unregister_console(con);
} }
} }
ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL, ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
console_cpu_notify); console_cpu_notify);
WARN_ON(ret < 0); WARN_ON(ret < 0);
......
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