Skip to content

Commit 2b1be68

Browse files
mpredfearnpmladek
authored andcommitted
printk/console: Always disable boot consoles that use init memory before it is freed
Commit 4c30c6f ("kernel/printk: do not turn off bootconsole in printk_late_init() if keep_bootcon") added a check on keep_bootcon to ensure that boot consoles were kept around until the real console is registered. This can lead to problems if the boot console data and code are in the init section, since it can be freed before the boot console is unregistered. Commit 81cc26f ("printk: only unregister boot consoles when necessary") fixed this a better way. It allowed to keep boot consoles that did not use init data. Unfortunately it did not remove the check of keep_bootcon. This can lead to crashes and weird panics when the bootconsole is accessed after free, especially if page poisoning is in use and the code / data have been overwritten with a poison value. To prevent this, always free the boot console if it is within the init section. In addition, print a warning about that the console is removed prematurely. Finally there is a new comment how to avoid the warning. It replaced an explanation that duplicated a more comprehensive function description few lines above. Fixes: 4c30c6f ("kernel/printk: do not turn off bootconsole in printk_late_init() if keep_bootcon") Link: http://lkml.kernel.org/r/[email protected] Cc: Steven Rostedt <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Jiri Slaby <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Alan Cox <[email protected]> Cc: "Fabio M. Di Nitto" <[email protected]> Cc: [email protected] Cc: [email protected] Signed-off-by: Matt Redfearn <[email protected]> [[email protected]: print the warning, code and comments clean up] Reviewed-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]>
1 parent aec47ca commit 2b1be68

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

kernel/printk/printk.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,26 +2650,24 @@ void __init console_init(void)
26502650
* makes it difficult to diagnose problems that occur during this time.
26512651
*
26522652
* To mitigate this problem somewhat, only unregister consoles whose memory
2653-
* intersects with the init section. Note that code exists elsewhere to get
2654-
* rid of the boot console as soon as the proper console shows up, so there
2655-
* won't be side-effects from postponing the removal.
2653+
* intersects with the init section. Note that all other boot consoles will
2654+
* get unregistred when the real preferred console is registered.
26562655
*/
26572656
static int __init printk_late_init(void)
26582657
{
26592658
struct console *con;
26602659
int ret;
26612660

26622661
for_each_console(con) {
2663-
if (!keep_bootcon && con->flags & CON_BOOT) {
2662+
if ((con->flags & CON_BOOT) &&
2663+
init_section_intersects(con, sizeof(*con))) {
26642664
/*
2665-
* Make sure to unregister boot consoles whose data
2666-
* resides in the init section before the init section
2667-
* is discarded. Boot consoles whose data will stick
2668-
* around will automatically be unregistered when the
2669-
* proper console replaces them.
2665+
* Please, consider moving the reported consoles out
2666+
* of the init section.
26702667
*/
2671-
if (init_section_intersects(con, sizeof(*con)))
2672-
unregister_console(con);
2668+
pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n",
2669+
con->name, con->index);
2670+
unregister_console(con);
26732671
}
26742672
}
26752673
ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,

0 commit comments

Comments
 (0)