Skip to content

Commit 81cc26f

Browse files
thierryredingtorvalds
authored andcommitted
printk: only unregister boot consoles when necessary
Boot consoles are typically replaced by proper consoles during the boot process. This can be problematic if the boot console data is part of the init section that is reclaimed late during boot. If the proper console does not register before this point in time, the boot console will need to be removed (so that the freed memory is not accessed), leaving the system without output for some time. There are various reasons why the proper console may not register early enough, such as deferred probe or the driver being a loadable module. If that happens, there is some amount of time where no console messages are visible to the user, which in turn can mean that they won't see crashes or other potentially useful information. To avoid this situation, only remove the boot console when it resides in the init section. Code exists to replace the boot console by the proper console when it is registered, keeping a seamless transition between the boot and proper consoles. Signed-off-by: Thierry Reding <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Joe Perches <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 9795593 commit 81cc26f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

kernel/printk/printk.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <linux/uio.h>
4949

5050
#include <asm/uaccess.h>
51+
#include <asm-generic/sections.h>
5152

5253
#define CREATE_TRACE_POINTS
5354
#include <trace/events/printk.h>
@@ -2658,13 +2659,36 @@ int unregister_console(struct console *console)
26582659
}
26592660
EXPORT_SYMBOL(unregister_console);
26602661

2662+
/*
2663+
* Some boot consoles access data that is in the init section and which will
2664+
* be discarded after the initcalls have been run. To make sure that no code
2665+
* will access this data, unregister the boot consoles in a late initcall.
2666+
*
2667+
* If for some reason, such as deferred probe or the driver being a loadable
2668+
* module, the real console hasn't registered yet at this point, there will
2669+
* be a brief interval in which no messages are logged to the console, which
2670+
* makes it difficult to diagnose problems that occur during this time.
2671+
*
2672+
* To mitigate this problem somewhat, only unregister consoles whose memory
2673+
* intersects with the init section. Note that code exists elsewhere to get
2674+
* rid of the boot console as soon as the proper console shows up, so there
2675+
* won't be side-effects from postponing the removal.
2676+
*/
26612677
static int __init printk_late_init(void)
26622678
{
26632679
struct console *con;
26642680

26652681
for_each_console(con) {
26662682
if (!keep_bootcon && con->flags & CON_BOOT) {
2667-
unregister_console(con);
2683+
/*
2684+
* Make sure to unregister boot consoles whose data
2685+
* resides in the init section before the init section
2686+
* is discarded. Boot consoles whose data will stick
2687+
* around will automatically be unregistered when the
2688+
* proper console replaces them.
2689+
*/
2690+
if (init_section_intersects(con, sizeof(*con)))
2691+
unregister_console(con);
26682692
}
26692693
}
26702694
hotcpu_notifier(console_cpu_notify, 0);

0 commit comments

Comments
 (0)