diff options
Diffstat (limited to 'kernel/printk.c')
-rw-r--r-- | kernel/printk.c | 480 |
1 files changed, 370 insertions, 110 deletions
diff --git a/kernel/printk.c b/kernel/printk.c index b4d97b54c1ec..f64b8997fc76 100644 --- a/kernel/printk.c +++ b/kernel/printk.c | |||
@@ -33,6 +33,12 @@ | |||
33 | #include <linux/bootmem.h> | 33 | #include <linux/bootmem.h> |
34 | #include <linux/syscalls.h> | 34 | #include <linux/syscalls.h> |
35 | #include <linux/kexec.h> | 35 | #include <linux/kexec.h> |
36 | #include <linux/kdb.h> | ||
37 | #include <linux/ratelimit.h> | ||
38 | #include <linux/kmsg_dump.h> | ||
39 | #include <linux/syslog.h> | ||
40 | #include <linux/cpu.h> | ||
41 | #include <linux/notifier.h> | ||
36 | 42 | ||
37 | #include <asm/uaccess.h> | 43 | #include <asm/uaccess.h> |
38 | 44 | ||
@@ -73,7 +79,7 @@ EXPORT_SYMBOL(oops_in_progress); | |||
73 | * provides serialisation for access to the entire console | 79 | * provides serialisation for access to the entire console |
74 | * driver system. | 80 | * driver system. |
75 | */ | 81 | */ |
76 | static DECLARE_MUTEX(console_sem); | 82 | static DEFINE_SEMAPHORE(console_sem); |
77 | struct console *console_drivers; | 83 | struct console *console_drivers; |
78 | EXPORT_SYMBOL_GPL(console_drivers); | 84 | EXPORT_SYMBOL_GPL(console_drivers); |
79 | 85 | ||
@@ -135,6 +141,7 @@ static char __log_buf[__LOG_BUF_LEN]; | |||
135 | static char *log_buf = __log_buf; | 141 | static char *log_buf = __log_buf; |
136 | static int log_buf_len = __LOG_BUF_LEN; | 142 | static int log_buf_len = __LOG_BUF_LEN; |
137 | static unsigned logged_chars; /* Number of chars produced since last read+clear operation */ | 143 | static unsigned logged_chars; /* Number of chars produced since last read+clear operation */ |
144 | static int saved_console_loglevel = -1; | ||
138 | 145 | ||
139 | #ifdef CONFIG_KEXEC | 146 | #ifdef CONFIG_KEXEC |
140 | /* | 147 | /* |
@@ -197,13 +204,12 @@ __setup("log_buf_len=", log_buf_len_setup); | |||
197 | 204 | ||
198 | #ifdef CONFIG_BOOT_PRINTK_DELAY | 205 | #ifdef CONFIG_BOOT_PRINTK_DELAY |
199 | 206 | ||
200 | static unsigned int boot_delay; /* msecs delay after each printk during bootup */ | 207 | static int boot_delay; /* msecs delay after each printk during bootup */ |
201 | static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */ | 208 | static unsigned long long loops_per_msec; /* based on boot_delay */ |
202 | 209 | ||
203 | static int __init boot_delay_setup(char *str) | 210 | static int __init boot_delay_setup(char *str) |
204 | { | 211 | { |
205 | unsigned long lpj; | 212 | unsigned long lpj; |
206 | unsigned long long loops_per_msec; | ||
207 | 213 | ||
208 | lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ | 214 | lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ |
209 | loops_per_msec = (unsigned long long)lpj / 1000 * HZ; | 215 | loops_per_msec = (unsigned long long)lpj / 1000 * HZ; |
@@ -212,10 +218,9 @@ static int __init boot_delay_setup(char *str) | |||
212 | if (boot_delay > 10 * 1000) | 218 | if (boot_delay > 10 * 1000) |
213 | boot_delay = 0; | 219 | boot_delay = 0; |
214 | 220 | ||
215 | printk_delay_msec = loops_per_msec; | 221 | pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " |
216 | printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, " | 222 | "HZ: %d, loops_per_msec: %llu\n", |
217 | "HZ: %d, printk_delay_msec: %llu\n", | 223 | boot_delay, preset_lpj, lpj, HZ, loops_per_msec); |
218 | boot_delay, preset_lpj, lpj, HZ, printk_delay_msec); | ||
219 | return 1; | 224 | return 1; |
220 | } | 225 | } |
221 | __setup("boot_delay=", boot_delay_setup); | 226 | __setup("boot_delay=", boot_delay_setup); |
@@ -228,7 +233,7 @@ static void boot_delay_msec(void) | |||
228 | if (boot_delay == 0 || system_state != SYSTEM_BOOTING) | 233 | if (boot_delay == 0 || system_state != SYSTEM_BOOTING) |
229 | return; | 234 | return; |
230 | 235 | ||
231 | k = (unsigned long long)printk_delay_msec * boot_delay; | 236 | k = (unsigned long long)loops_per_msec * boot_delay; |
232 | 237 | ||
233 | timeout = jiffies + msecs_to_jiffies(boot_delay); | 238 | timeout = jiffies + msecs_to_jiffies(boot_delay); |
234 | while (k) { | 239 | while (k) { |
@@ -250,38 +255,42 @@ static inline void boot_delay_msec(void) | |||
250 | } | 255 | } |
251 | #endif | 256 | #endif |
252 | 257 | ||
253 | /* | 258 | #ifdef CONFIG_SECURITY_DMESG_RESTRICT |
254 | * Commands to do_syslog: | 259 | int dmesg_restrict = 1; |
255 | * | 260 | #else |
256 | * 0 -- Close the log. Currently a NOP. | 261 | int dmesg_restrict; |
257 | * 1 -- Open the log. Currently a NOP. | 262 | #endif |
258 | * 2 -- Read from the log. | 263 | |
259 | * 3 -- Read all messages remaining in the ring buffer. | 264 | int do_syslog(int type, char __user *buf, int len, bool from_file) |
260 | * 4 -- Read and clear all messages remaining in the ring buffer | ||
261 | * 5 -- Clear ring buffer. | ||
262 | * 6 -- Disable printk's to console | ||
263 | * 7 -- Enable printk's to console | ||
264 | * 8 -- Set level of messages printed to console | ||
265 | * 9 -- Return number of unread characters in the log buffer | ||
266 | * 10 -- Return size of the log buffer | ||
267 | */ | ||
268 | int do_syslog(int type, char __user *buf, int len) | ||
269 | { | 265 | { |
270 | unsigned i, j, limit, count; | 266 | unsigned i, j, limit, count; |
271 | int do_clear = 0; | 267 | int do_clear = 0; |
272 | char c; | 268 | char c; |
273 | int error = 0; | 269 | int error = 0; |
274 | 270 | ||
271 | /* | ||
272 | * If this is from /proc/kmsg we only do the capabilities checks | ||
273 | * at open time. | ||
274 | */ | ||
275 | if (type == SYSLOG_ACTION_OPEN || !from_file) { | ||
276 | if (dmesg_restrict && !capable(CAP_SYSLOG)) | ||
277 | goto warn; /* switch to return -EPERM after 2.6.39 */ | ||
278 | if ((type != SYSLOG_ACTION_READ_ALL && | ||
279 | type != SYSLOG_ACTION_SIZE_BUFFER) && | ||
280 | !capable(CAP_SYSLOG)) | ||
281 | goto warn; /* switch to return -EPERM after 2.6.39 */ | ||
282 | } | ||
283 | |||
275 | error = security_syslog(type); | 284 | error = security_syslog(type); |
276 | if (error) | 285 | if (error) |
277 | return error; | 286 | return error; |
278 | 287 | ||
279 | switch (type) { | 288 | switch (type) { |
280 | case 0: /* Close log */ | 289 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
281 | break; | 290 | break; |
282 | case 1: /* Open log */ | 291 | case SYSLOG_ACTION_OPEN: /* Open log */ |
283 | break; | 292 | break; |
284 | case 2: /* Read from log */ | 293 | case SYSLOG_ACTION_READ: /* Read from log */ |
285 | error = -EINVAL; | 294 | error = -EINVAL; |
286 | if (!buf || len < 0) | 295 | if (!buf || len < 0) |
287 | goto out; | 296 | goto out; |
@@ -312,10 +321,12 @@ int do_syslog(int type, char __user *buf, int len) | |||
312 | if (!error) | 321 | if (!error) |
313 | error = i; | 322 | error = i; |
314 | break; | 323 | break; |
315 | case 4: /* Read/clear last kernel messages */ | 324 | /* Read/clear last kernel messages */ |
325 | case SYSLOG_ACTION_READ_CLEAR: | ||
316 | do_clear = 1; | 326 | do_clear = 1; |
317 | /* FALL THRU */ | 327 | /* FALL THRU */ |
318 | case 3: /* Read last kernel messages */ | 328 | /* Read last kernel messages */ |
329 | case SYSLOG_ACTION_READ_ALL: | ||
319 | error = -EINVAL; | 330 | error = -EINVAL; |
320 | if (!buf || len < 0) | 331 | if (!buf || len < 0) |
321 | goto out; | 332 | goto out; |
@@ -368,28 +379,41 @@ int do_syslog(int type, char __user *buf, int len) | |||
368 | } | 379 | } |
369 | } | 380 | } |
370 | break; | 381 | break; |
371 | case 5: /* Clear ring buffer */ | 382 | /* Clear ring buffer */ |
383 | case SYSLOG_ACTION_CLEAR: | ||
372 | logged_chars = 0; | 384 | logged_chars = 0; |
373 | break; | 385 | break; |
374 | case 6: /* Disable logging to console */ | 386 | /* Disable logging to console */ |
387 | case SYSLOG_ACTION_CONSOLE_OFF: | ||
388 | if (saved_console_loglevel == -1) | ||
389 | saved_console_loglevel = console_loglevel; | ||
375 | console_loglevel = minimum_console_loglevel; | 390 | console_loglevel = minimum_console_loglevel; |
376 | break; | 391 | break; |
377 | case 7: /* Enable logging to console */ | 392 | /* Enable logging to console */ |
378 | console_loglevel = default_console_loglevel; | 393 | case SYSLOG_ACTION_CONSOLE_ON: |
394 | if (saved_console_loglevel != -1) { | ||
395 | console_loglevel = saved_console_loglevel; | ||
396 | saved_console_loglevel = -1; | ||
397 | } | ||
379 | break; | 398 | break; |
380 | case 8: /* Set level of messages printed to console */ | 399 | /* Set level of messages printed to console */ |
400 | case SYSLOG_ACTION_CONSOLE_LEVEL: | ||
381 | error = -EINVAL; | 401 | error = -EINVAL; |
382 | if (len < 1 || len > 8) | 402 | if (len < 1 || len > 8) |
383 | goto out; | 403 | goto out; |
384 | if (len < minimum_console_loglevel) | 404 | if (len < minimum_console_loglevel) |
385 | len = minimum_console_loglevel; | 405 | len = minimum_console_loglevel; |
386 | console_loglevel = len; | 406 | console_loglevel = len; |
407 | /* Implicitly re-enable logging to console */ | ||
408 | saved_console_loglevel = -1; | ||
387 | error = 0; | 409 | error = 0; |
388 | break; | 410 | break; |
389 | case 9: /* Number of chars in the log buffer */ | 411 | /* Number of chars in the log buffer */ |
412 | case SYSLOG_ACTION_SIZE_UNREAD: | ||
390 | error = log_end - log_start; | 413 | error = log_end - log_start; |
391 | break; | 414 | break; |
392 | case 10: /* Size of the log buffer */ | 415 | /* Size of the log buffer */ |
416 | case SYSLOG_ACTION_SIZE_BUFFER: | ||
393 | error = log_buf_len; | 417 | error = log_buf_len; |
394 | break; | 418 | break; |
395 | default: | 419 | default: |
@@ -398,13 +422,35 @@ int do_syslog(int type, char __user *buf, int len) | |||
398 | } | 422 | } |
399 | out: | 423 | out: |
400 | return error; | 424 | return error; |
425 | warn: | ||
426 | /* remove after 2.6.39 */ | ||
427 | if (capable(CAP_SYS_ADMIN)) | ||
428 | WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN " | ||
429 | "but no CAP_SYSLOG (deprecated and denied).\n"); | ||
430 | return -EPERM; | ||
401 | } | 431 | } |
402 | 432 | ||
403 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) | 433 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) |
404 | { | 434 | { |
405 | return do_syslog(type, buf, len); | 435 | return do_syslog(type, buf, len, SYSLOG_FROM_CALL); |
406 | } | 436 | } |
407 | 437 | ||
438 | #ifdef CONFIG_KGDB_KDB | ||
439 | /* kdb dmesg command needs access to the syslog buffer. do_syslog() | ||
440 | * uses locks so it cannot be used during debugging. Just tell kdb | ||
441 | * where the start and end of the physical and logical logs are. This | ||
442 | * is equivalent to do_syslog(3). | ||
443 | */ | ||
444 | void kdb_syslog_data(char *syslog_data[4]) | ||
445 | { | ||
446 | syslog_data[0] = log_buf; | ||
447 | syslog_data[1] = log_buf + log_buf_len; | ||
448 | syslog_data[2] = log_buf + log_end - | ||
449 | (logged_chars < log_buf_len ? logged_chars : log_buf_len); | ||
450 | syslog_data[3] = log_buf + log_end; | ||
451 | } | ||
452 | #endif /* CONFIG_KGDB_KDB */ | ||
453 | |||
408 | /* | 454 | /* |
409 | * Call the console drivers on a range of log_buf | 455 | * Call the console drivers on a range of log_buf |
410 | */ | 456 | */ |
@@ -412,7 +458,7 @@ static void __call_console_drivers(unsigned start, unsigned end) | |||
412 | { | 458 | { |
413 | struct console *con; | 459 | struct console *con; |
414 | 460 | ||
415 | for (con = console_drivers; con; con = con->next) { | 461 | for_each_console(con) { |
416 | if ((con->flags & CON_ENABLED) && con->write && | 462 | if ((con->flags & CON_ENABLED) && con->write && |
417 | (cpu_online(smp_processor_id()) || | 463 | (cpu_online(smp_processor_id()) || |
418 | (con->flags & CON_ANYTIME))) | 464 | (con->flags & CON_ANYTIME))) |
@@ -529,7 +575,7 @@ static void zap_locks(void) | |||
529 | /* If a crash is occurring, make sure we can't deadlock */ | 575 | /* If a crash is occurring, make sure we can't deadlock */ |
530 | spin_lock_init(&logbuf_lock); | 576 | spin_lock_init(&logbuf_lock); |
531 | /* And make sure that we print immediately */ | 577 | /* And make sure that we print immediately */ |
532 | init_MUTEX(&console_sem); | 578 | sema_init(&console_sem, 1); |
533 | } | 579 | } |
534 | 580 | ||
535 | #if defined(CONFIG_PRINTK_TIME) | 581 | #if defined(CONFIG_PRINTK_TIME) |
@@ -544,7 +590,7 @@ static int have_callable_console(void) | |||
544 | { | 590 | { |
545 | struct console *con; | 591 | struct console *con; |
546 | 592 | ||
547 | for (con = console_drivers; con; con = con->next) | 593 | for_each_console(con) |
548 | if (con->flags & CON_ANYTIME) | 594 | if (con->flags & CON_ANYTIME) |
549 | return 1; | 595 | return 1; |
550 | 596 | ||
@@ -578,6 +624,14 @@ asmlinkage int printk(const char *fmt, ...) | |||
578 | va_list args; | 624 | va_list args; |
579 | int r; | 625 | int r; |
580 | 626 | ||
627 | #ifdef CONFIG_KGDB_KDB | ||
628 | if (unlikely(kdb_trap_printk)) { | ||
629 | va_start(args, fmt); | ||
630 | r = vkdb_printf(fmt, args); | ||
631 | va_end(args); | ||
632 | return r; | ||
633 | } | ||
634 | #endif | ||
581 | va_start(args, fmt); | 635 | va_start(args, fmt); |
582 | r = vprintk(fmt, args); | 636 | r = vprintk(fmt, args); |
583 | va_end(args); | 637 | va_end(args); |
@@ -612,6 +666,7 @@ static inline int can_use_console(unsigned int cpu) | |||
612 | * released but interrupts still disabled. | 666 | * released but interrupts still disabled. |
613 | */ | 667 | */ |
614 | static int acquire_console_semaphore_for_printk(unsigned int cpu) | 668 | static int acquire_console_semaphore_for_printk(unsigned int cpu) |
669 | __releases(&logbuf_lock) | ||
615 | { | 670 | { |
616 | int retval = 0; | 671 | int retval = 0; |
617 | 672 | ||
@@ -640,6 +695,20 @@ static int recursion_bug; | |||
640 | static int new_text_line = 1; | 695 | static int new_text_line = 1; |
641 | static char printk_buf[1024]; | 696 | static char printk_buf[1024]; |
642 | 697 | ||
698 | int printk_delay_msec __read_mostly; | ||
699 | |||
700 | static inline void printk_delay(void) | ||
701 | { | ||
702 | if (unlikely(printk_delay_msec)) { | ||
703 | int m = printk_delay_msec; | ||
704 | |||
705 | while (m--) { | ||
706 | mdelay(1); | ||
707 | touch_nmi_watchdog(); | ||
708 | } | ||
709 | } | ||
710 | } | ||
711 | |||
643 | asmlinkage int vprintk(const char *fmt, va_list args) | 712 | asmlinkage int vprintk(const char *fmt, va_list args) |
644 | { | 713 | { |
645 | int printed_len = 0; | 714 | int printed_len = 0; |
@@ -649,6 +718,7 @@ asmlinkage int vprintk(const char *fmt, va_list args) | |||
649 | char *p; | 718 | char *p; |
650 | 719 | ||
651 | boot_delay_msec(); | 720 | boot_delay_msec(); |
721 | printk_delay(); | ||
652 | 722 | ||
653 | preempt_disable(); | 723 | preempt_disable(); |
654 | /* This stops the holder of console_sem just where we want him */ | 724 | /* This stops the holder of console_sem just where we want him */ |
@@ -937,6 +1007,32 @@ void resume_console(void) | |||
937 | } | 1007 | } |
938 | 1008 | ||
939 | /** | 1009 | /** |
1010 | * console_cpu_notify - print deferred console messages after CPU hotplug | ||
1011 | * @self: notifier struct | ||
1012 | * @action: CPU hotplug event | ||
1013 | * @hcpu: unused | ||
1014 | * | ||
1015 | * If printk() is called from a CPU that is not online yet, the messages | ||
1016 | * will be spooled but will not show up on the console. This function is | ||
1017 | * called when a new CPU comes online (or fails to come up), and ensures | ||
1018 | * that any such output gets printed. | ||
1019 | */ | ||
1020 | static int __cpuinit console_cpu_notify(struct notifier_block *self, | ||
1021 | unsigned long action, void *hcpu) | ||
1022 | { | ||
1023 | switch (action) { | ||
1024 | case CPU_ONLINE: | ||
1025 | case CPU_DEAD: | ||
1026 | case CPU_DYING: | ||
1027 | case CPU_DOWN_FAILED: | ||
1028 | case CPU_UP_CANCELED: | ||
1029 | acquire_console_sem(); | ||
1030 | release_console_sem(); | ||
1031 | } | ||
1032 | return NOTIFY_OK; | ||
1033 | } | ||
1034 | |||
1035 | /** | ||
940 | * acquire_console_sem - lock the console system for exclusive use. | 1036 | * acquire_console_sem - lock the console system for exclusive use. |
941 | * | 1037 | * |
942 | * Acquires a semaphore which guarantees that the caller has | 1038 | * Acquires a semaphore which guarantees that the caller has |
@@ -978,21 +1074,23 @@ static DEFINE_PER_CPU(int, printk_pending); | |||
978 | 1074 | ||
979 | void printk_tick(void) | 1075 | void printk_tick(void) |
980 | { | 1076 | { |
981 | if (__get_cpu_var(printk_pending)) { | 1077 | if (__this_cpu_read(printk_pending)) { |
982 | __get_cpu_var(printk_pending) = 0; | 1078 | __this_cpu_write(printk_pending, 0); |
983 | wake_up_interruptible(&log_wait); | 1079 | wake_up_interruptible(&log_wait); |
984 | } | 1080 | } |
985 | } | 1081 | } |
986 | 1082 | ||
987 | int printk_needs_cpu(int cpu) | 1083 | int printk_needs_cpu(int cpu) |
988 | { | 1084 | { |
989 | return per_cpu(printk_pending, cpu); | 1085 | if (cpu_is_offline(cpu)) |
1086 | printk_tick(); | ||
1087 | return __this_cpu_read(printk_pending); | ||
990 | } | 1088 | } |
991 | 1089 | ||
992 | void wake_up_klogd(void) | 1090 | void wake_up_klogd(void) |
993 | { | 1091 | { |
994 | if (waitqueue_active(&log_wait)) | 1092 | if (waitqueue_active(&log_wait)) |
995 | __raw_get_cpu_var(printk_pending) = 1; | 1093 | this_cpu_write(printk_pending, 1); |
996 | } | 1094 | } |
997 | 1095 | ||
998 | /** | 1096 | /** |
@@ -1060,12 +1158,6 @@ void __sched console_conditional_schedule(void) | |||
1060 | } | 1158 | } |
1061 | EXPORT_SYMBOL(console_conditional_schedule); | 1159 | EXPORT_SYMBOL(console_conditional_schedule); |
1062 | 1160 | ||
1063 | void console_print(const char *s) | ||
1064 | { | ||
1065 | printk(KERN_EMERG "%s", s); | ||
1066 | } | ||
1067 | EXPORT_SYMBOL(console_print); | ||
1068 | |||
1069 | void console_unblank(void) | 1161 | void console_unblank(void) |
1070 | { | 1162 | { |
1071 | struct console *c; | 1163 | struct console *c; |
@@ -1082,7 +1174,7 @@ void console_unblank(void) | |||
1082 | 1174 | ||
1083 | console_locked = 1; | 1175 | console_locked = 1; |
1084 | console_may_schedule = 0; | 1176 | console_may_schedule = 0; |
1085 | for (c = console_drivers; c != NULL; c = c->next) | 1177 | for_each_console(c) |
1086 | if ((c->flags & CON_ENABLED) && c->unblank) | 1178 | if ((c->flags & CON_ENABLED) && c->unblank) |
1087 | c->unblank(); | 1179 | c->unblank(); |
1088 | release_console_sem(); | 1180 | release_console_sem(); |
@@ -1097,7 +1189,7 @@ struct tty_driver *console_device(int *index) | |||
1097 | struct tty_driver *driver = NULL; | 1189 | struct tty_driver *driver = NULL; |
1098 | 1190 | ||
1099 | acquire_console_sem(); | 1191 | acquire_console_sem(); |
1100 | for (c = console_drivers; c != NULL; c = c->next) { | 1192 | for_each_console(c) { |
1101 | if (!c->device) | 1193 | if (!c->device) |
1102 | continue; | 1194 | continue; |
1103 | driver = c->device(c, index); | 1195 | driver = c->device(c, index); |
@@ -1134,25 +1226,49 @@ EXPORT_SYMBOL(console_start); | |||
1134 | * to register the console printing procedure with printk() and to | 1226 | * to register the console printing procedure with printk() and to |
1135 | * print any messages that were printed by the kernel before the | 1227 | * print any messages that were printed by the kernel before the |
1136 | * console driver was initialized. | 1228 | * console driver was initialized. |
1229 | * | ||
1230 | * This can happen pretty early during the boot process (because of | ||
1231 | * early_printk) - sometimes before setup_arch() completes - be careful | ||
1232 | * of what kernel features are used - they may not be initialised yet. | ||
1233 | * | ||
1234 | * There are two types of consoles - bootconsoles (early_printk) and | ||
1235 | * "real" consoles (everything which is not a bootconsole) which are | ||
1236 | * handled differently. | ||
1237 | * - Any number of bootconsoles can be registered at any time. | ||
1238 | * - As soon as a "real" console is registered, all bootconsoles | ||
1239 | * will be unregistered automatically. | ||
1240 | * - Once a "real" console is registered, any attempt to register a | ||
1241 | * bootconsoles will be rejected | ||
1137 | */ | 1242 | */ |
1138 | void register_console(struct console *console) | 1243 | void register_console(struct console *newcon) |
1139 | { | 1244 | { |
1140 | int i; | 1245 | int i; |
1141 | unsigned long flags; | 1246 | unsigned long flags; |
1142 | struct console *bootconsole = NULL; | 1247 | struct console *bcon = NULL; |
1143 | 1248 | ||
1144 | if (console_drivers) { | 1249 | /* |
1145 | if (console->flags & CON_BOOT) | 1250 | * before we register a new CON_BOOT console, make sure we don't |
1146 | return; | 1251 | * already have a valid console |
1147 | if (console_drivers->flags & CON_BOOT) | 1252 | */ |
1148 | bootconsole = console_drivers; | 1253 | if (console_drivers && newcon->flags & CON_BOOT) { |
1254 | /* find the last or real console */ | ||
1255 | for_each_console(bcon) { | ||
1256 | if (!(bcon->flags & CON_BOOT)) { | ||
1257 | printk(KERN_INFO "Too late to register bootconsole %s%d\n", | ||
1258 | newcon->name, newcon->index); | ||
1259 | return; | ||
1260 | } | ||
1261 | } | ||
1149 | } | 1262 | } |
1150 | 1263 | ||
1151 | if (preferred_console < 0 || bootconsole || !console_drivers) | 1264 | if (console_drivers && console_drivers->flags & CON_BOOT) |
1265 | bcon = console_drivers; | ||
1266 | |||
1267 | if (preferred_console < 0 || bcon || !console_drivers) | ||
1152 | preferred_console = selected_console; | 1268 | preferred_console = selected_console; |
1153 | 1269 | ||
1154 | if (console->early_setup) | 1270 | if (newcon->early_setup) |
1155 | console->early_setup(); | 1271 | newcon->early_setup(); |
1156 | 1272 | ||
1157 | /* | 1273 | /* |
1158 | * See if we want to use this console driver. If we | 1274 | * See if we want to use this console driver. If we |
@@ -1160,13 +1276,13 @@ void register_console(struct console *console) | |||
1160 | * that registers here. | 1276 | * that registers here. |
1161 | */ | 1277 | */ |
1162 | if (preferred_console < 0) { | 1278 | if (preferred_console < 0) { |
1163 | if (console->index < 0) | 1279 | if (newcon->index < 0) |
1164 | console->index = 0; | 1280 | newcon->index = 0; |
1165 | if (console->setup == NULL || | 1281 | if (newcon->setup == NULL || |
1166 | console->setup(console, NULL) == 0) { | 1282 | newcon->setup(newcon, NULL) == 0) { |
1167 | console->flags |= CON_ENABLED; | 1283 | newcon->flags |= CON_ENABLED; |
1168 | if (console->device) { | 1284 | if (newcon->device) { |
1169 | console->flags |= CON_CONSDEV; | 1285 | newcon->flags |= CON_CONSDEV; |
1170 | preferred_console = 0; | 1286 | preferred_console = 0; |
1171 | } | 1287 | } |
1172 | } | 1288 | } |
@@ -1178,64 +1294,62 @@ void register_console(struct console *console) | |||
1178 | */ | 1294 | */ |
1179 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; | 1295 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; |
1180 | i++) { | 1296 | i++) { |
1181 | if (strcmp(console_cmdline[i].name, console->name) != 0) | 1297 | if (strcmp(console_cmdline[i].name, newcon->name) != 0) |
1182 | continue; | 1298 | continue; |
1183 | if (console->index >= 0 && | 1299 | if (newcon->index >= 0 && |
1184 | console->index != console_cmdline[i].index) | 1300 | newcon->index != console_cmdline[i].index) |
1185 | continue; | 1301 | continue; |
1186 | if (console->index < 0) | 1302 | if (newcon->index < 0) |
1187 | console->index = console_cmdline[i].index; | 1303 | newcon->index = console_cmdline[i].index; |
1188 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE | 1304 | #ifdef CONFIG_A11Y_BRAILLE_CONSOLE |
1189 | if (console_cmdline[i].brl_options) { | 1305 | if (console_cmdline[i].brl_options) { |
1190 | console->flags |= CON_BRL; | 1306 | newcon->flags |= CON_BRL; |
1191 | braille_register_console(console, | 1307 | braille_register_console(newcon, |
1192 | console_cmdline[i].index, | 1308 | console_cmdline[i].index, |
1193 | console_cmdline[i].options, | 1309 | console_cmdline[i].options, |
1194 | console_cmdline[i].brl_options); | 1310 | console_cmdline[i].brl_options); |
1195 | return; | 1311 | return; |
1196 | } | 1312 | } |
1197 | #endif | 1313 | #endif |
1198 | if (console->setup && | 1314 | if (newcon->setup && |
1199 | console->setup(console, console_cmdline[i].options) != 0) | 1315 | newcon->setup(newcon, console_cmdline[i].options) != 0) |
1200 | break; | 1316 | break; |
1201 | console->flags |= CON_ENABLED; | 1317 | newcon->flags |= CON_ENABLED; |
1202 | console->index = console_cmdline[i].index; | 1318 | newcon->index = console_cmdline[i].index; |
1203 | if (i == selected_console) { | 1319 | if (i == selected_console) { |
1204 | console->flags |= CON_CONSDEV; | 1320 | newcon->flags |= CON_CONSDEV; |
1205 | preferred_console = selected_console; | 1321 | preferred_console = selected_console; |
1206 | } | 1322 | } |
1207 | break; | 1323 | break; |
1208 | } | 1324 | } |
1209 | 1325 | ||
1210 | if (!(console->flags & CON_ENABLED)) | 1326 | if (!(newcon->flags & CON_ENABLED)) |
1211 | return; | 1327 | return; |
1212 | 1328 | ||
1213 | if (bootconsole && (console->flags & CON_CONSDEV)) { | 1329 | /* |
1214 | printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n", | 1330 | * If we have a bootconsole, and are switching to a real console, |
1215 | bootconsole->name, bootconsole->index, | 1331 | * don't print everything out again, since when the boot console, and |
1216 | console->name, console->index); | 1332 | * the real console are the same physical device, it's annoying to |
1217 | unregister_console(bootconsole); | 1333 | * see the beginning boot messages twice |
1218 | console->flags &= ~CON_PRINTBUFFER; | 1334 | */ |
1219 | } else { | 1335 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) |
1220 | printk(KERN_INFO "console [%s%d] enabled\n", | 1336 | newcon->flags &= ~CON_PRINTBUFFER; |
1221 | console->name, console->index); | ||
1222 | } | ||
1223 | 1337 | ||
1224 | /* | 1338 | /* |
1225 | * Put this console in the list - keep the | 1339 | * Put this console in the list - keep the |
1226 | * preferred driver at the head of the list. | 1340 | * preferred driver at the head of the list. |
1227 | */ | 1341 | */ |
1228 | acquire_console_sem(); | 1342 | acquire_console_sem(); |
1229 | if ((console->flags & CON_CONSDEV) || console_drivers == NULL) { | 1343 | if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { |
1230 | console->next = console_drivers; | 1344 | newcon->next = console_drivers; |
1231 | console_drivers = console; | 1345 | console_drivers = newcon; |
1232 | if (console->next) | 1346 | if (newcon->next) |
1233 | console->next->flags &= ~CON_CONSDEV; | 1347 | newcon->next->flags &= ~CON_CONSDEV; |
1234 | } else { | 1348 | } else { |
1235 | console->next = console_drivers->next; | 1349 | newcon->next = console_drivers->next; |
1236 | console_drivers->next = console; | 1350 | console_drivers->next = newcon; |
1237 | } | 1351 | } |
1238 | if (console->flags & CON_PRINTBUFFER) { | 1352 | if (newcon->flags & CON_PRINTBUFFER) { |
1239 | /* | 1353 | /* |
1240 | * release_console_sem() will print out the buffered messages | 1354 | * release_console_sem() will print out the buffered messages |
1241 | * for us. | 1355 | * for us. |
@@ -1245,6 +1359,29 @@ void register_console(struct console *console) | |||
1245 | spin_unlock_irqrestore(&logbuf_lock, flags); | 1359 | spin_unlock_irqrestore(&logbuf_lock, flags); |
1246 | } | 1360 | } |
1247 | release_console_sem(); | 1361 | release_console_sem(); |
1362 | console_sysfs_notify(); | ||
1363 | |||
1364 | /* | ||
1365 | * By unregistering the bootconsoles after we enable the real console | ||
1366 | * we get the "console xxx enabled" message on all the consoles - | ||
1367 | * boot consoles, real consoles, etc - this is to ensure that end | ||
1368 | * users know there might be something in the kernel's log buffer that | ||
1369 | * went to the bootconsole (that they do not see on the real console) | ||
1370 | */ | ||
1371 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) { | ||
1372 | /* we need to iterate through twice, to make sure we print | ||
1373 | * everything out, before we unregister the console(s) | ||
1374 | */ | ||
1375 | printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n", | ||
1376 | newcon->name, newcon->index); | ||
1377 | for_each_console(bcon) | ||
1378 | if (bcon->flags & CON_BOOT) | ||
1379 | unregister_console(bcon); | ||
1380 | } else { | ||
1381 | printk(KERN_INFO "%sconsole [%s%d] enabled\n", | ||
1382 | (newcon->flags & CON_BOOT) ? "boot" : "" , | ||
1383 | newcon->name, newcon->index); | ||
1384 | } | ||
1248 | } | 1385 | } |
1249 | EXPORT_SYMBOL(register_console); | 1386 | EXPORT_SYMBOL(register_console); |
1250 | 1387 | ||
@@ -1281,22 +1418,26 @@ int unregister_console(struct console *console) | |||
1281 | console_drivers->flags |= CON_CONSDEV; | 1418 | console_drivers->flags |= CON_CONSDEV; |
1282 | 1419 | ||
1283 | release_console_sem(); | 1420 | release_console_sem(); |
1421 | console_sysfs_notify(); | ||
1284 | return res; | 1422 | return res; |
1285 | } | 1423 | } |
1286 | EXPORT_SYMBOL(unregister_console); | 1424 | EXPORT_SYMBOL(unregister_console); |
1287 | 1425 | ||
1288 | static int __init disable_boot_consoles(void) | 1426 | static int __init printk_late_init(void) |
1289 | { | 1427 | { |
1290 | if (console_drivers != NULL) { | 1428 | struct console *con; |
1291 | if (console_drivers->flags & CON_BOOT) { | 1429 | |
1430 | for_each_console(con) { | ||
1431 | if (con->flags & CON_BOOT) { | ||
1292 | printk(KERN_INFO "turn off boot console %s%d\n", | 1432 | printk(KERN_INFO "turn off boot console %s%d\n", |
1293 | console_drivers->name, console_drivers->index); | 1433 | con->name, con->index); |
1294 | return unregister_console(console_drivers); | 1434 | unregister_console(con); |
1295 | } | 1435 | } |
1296 | } | 1436 | } |
1437 | hotcpu_notifier(console_cpu_notify, 0); | ||
1297 | return 0; | 1438 | return 0; |
1298 | } | 1439 | } |
1299 | late_initcall(disable_boot_consoles); | 1440 | late_initcall(printk_late_init); |
1300 | 1441 | ||
1301 | #if defined CONFIG_PRINTK | 1442 | #if defined CONFIG_PRINTK |
1302 | 1443 | ||
@@ -1308,11 +1449,11 @@ late_initcall(disable_boot_consoles); | |||
1308 | */ | 1449 | */ |
1309 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); | 1450 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); |
1310 | 1451 | ||
1311 | int printk_ratelimit(void) | 1452 | int __printk_ratelimit(const char *func) |
1312 | { | 1453 | { |
1313 | return __ratelimit(&printk_ratelimit_state); | 1454 | return ___ratelimit(&printk_ratelimit_state, func); |
1314 | } | 1455 | } |
1315 | EXPORT_SYMBOL(printk_ratelimit); | 1456 | EXPORT_SYMBOL(__printk_ratelimit); |
1316 | 1457 | ||
1317 | /** | 1458 | /** |
1318 | * printk_timed_ratelimit - caller-controlled printk ratelimiting | 1459 | * printk_timed_ratelimit - caller-controlled printk ratelimiting |
@@ -1336,4 +1477,123 @@ bool printk_timed_ratelimit(unsigned long *caller_jiffies, | |||
1336 | return false; | 1477 | return false; |
1337 | } | 1478 | } |
1338 | EXPORT_SYMBOL(printk_timed_ratelimit); | 1479 | EXPORT_SYMBOL(printk_timed_ratelimit); |
1480 | |||
1481 | static DEFINE_SPINLOCK(dump_list_lock); | ||
1482 | static LIST_HEAD(dump_list); | ||
1483 | |||
1484 | /** | ||
1485 | * kmsg_dump_register - register a kernel log dumper. | ||
1486 | * @dumper: pointer to the kmsg_dumper structure | ||
1487 | * | ||
1488 | * Adds a kernel log dumper to the system. The dump callback in the | ||
1489 | * structure will be called when the kernel oopses or panics and must be | ||
1490 | * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise. | ||
1491 | */ | ||
1492 | int kmsg_dump_register(struct kmsg_dumper *dumper) | ||
1493 | { | ||
1494 | unsigned long flags; | ||
1495 | int err = -EBUSY; | ||
1496 | |||
1497 | /* The dump callback needs to be set */ | ||
1498 | if (!dumper->dump) | ||
1499 | return -EINVAL; | ||
1500 | |||
1501 | spin_lock_irqsave(&dump_list_lock, flags); | ||
1502 | /* Don't allow registering multiple times */ | ||
1503 | if (!dumper->registered) { | ||
1504 | dumper->registered = 1; | ||
1505 | list_add_tail(&dumper->list, &dump_list); | ||
1506 | err = 0; | ||
1507 | } | ||
1508 | spin_unlock_irqrestore(&dump_list_lock, flags); | ||
1509 | |||
1510 | return err; | ||
1511 | } | ||
1512 | EXPORT_SYMBOL_GPL(kmsg_dump_register); | ||
1513 | |||
1514 | /** | ||
1515 | * kmsg_dump_unregister - unregister a kmsg dumper. | ||
1516 | * @dumper: pointer to the kmsg_dumper structure | ||
1517 | * | ||
1518 | * Removes a dump device from the system. Returns zero on success and | ||
1519 | * %-EINVAL otherwise. | ||
1520 | */ | ||
1521 | int kmsg_dump_unregister(struct kmsg_dumper *dumper) | ||
1522 | { | ||
1523 | unsigned long flags; | ||
1524 | int err = -EINVAL; | ||
1525 | |||
1526 | spin_lock_irqsave(&dump_list_lock, flags); | ||
1527 | if (dumper->registered) { | ||
1528 | dumper->registered = 0; | ||
1529 | list_del(&dumper->list); | ||
1530 | err = 0; | ||
1531 | } | ||
1532 | spin_unlock_irqrestore(&dump_list_lock, flags); | ||
1533 | |||
1534 | return err; | ||
1535 | } | ||
1536 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); | ||
1537 | |||
1538 | static const char * const kmsg_reasons[] = { | ||
1539 | [KMSG_DUMP_OOPS] = "oops", | ||
1540 | [KMSG_DUMP_PANIC] = "panic", | ||
1541 | [KMSG_DUMP_KEXEC] = "kexec", | ||
1542 | }; | ||
1543 | |||
1544 | static const char *kmsg_to_str(enum kmsg_dump_reason reason) | ||
1545 | { | ||
1546 | if (reason >= ARRAY_SIZE(kmsg_reasons) || reason < 0) | ||
1547 | return "unknown"; | ||
1548 | |||
1549 | return kmsg_reasons[reason]; | ||
1550 | } | ||
1551 | |||
1552 | /** | ||
1553 | * kmsg_dump - dump kernel log to kernel message dumpers. | ||
1554 | * @reason: the reason (oops, panic etc) for dumping | ||
1555 | * | ||
1556 | * Iterate through each of the dump devices and call the oops/panic | ||
1557 | * callbacks with the log buffer. | ||
1558 | */ | ||
1559 | void kmsg_dump(enum kmsg_dump_reason reason) | ||
1560 | { | ||
1561 | unsigned long end; | ||
1562 | unsigned chars; | ||
1563 | struct kmsg_dumper *dumper; | ||
1564 | const char *s1, *s2; | ||
1565 | unsigned long l1, l2; | ||
1566 | unsigned long flags; | ||
1567 | |||
1568 | /* Theoretically, the log could move on after we do this, but | ||
1569 | there's not a lot we can do about that. The new messages | ||
1570 | will overwrite the start of what we dump. */ | ||
1571 | spin_lock_irqsave(&logbuf_lock, flags); | ||
1572 | end = log_end & LOG_BUF_MASK; | ||
1573 | chars = logged_chars; | ||
1574 | spin_unlock_irqrestore(&logbuf_lock, flags); | ||
1575 | |||
1576 | if (chars > end) { | ||
1577 | s1 = log_buf + log_buf_len - chars + end; | ||
1578 | l1 = chars - end; | ||
1579 | |||
1580 | s2 = log_buf; | ||
1581 | l2 = end; | ||
1582 | } else { | ||
1583 | s1 = ""; | ||
1584 | l1 = 0; | ||
1585 | |||
1586 | s2 = log_buf + end - chars; | ||
1587 | l2 = chars; | ||
1588 | } | ||
1589 | |||
1590 | if (!spin_trylock_irqsave(&dump_list_lock, flags)) { | ||
1591 | printk(KERN_ERR "dump_kmsg: dump list lock is held during %s, skipping dump\n", | ||
1592 | kmsg_to_str(reason)); | ||
1593 | return; | ||
1594 | } | ||
1595 | list_for_each_entry(dumper, &dump_list, list) | ||
1596 | dumper->dump(dumper, reason, s1, l1, s2, l2); | ||
1597 | spin_unlock_irqrestore(&dump_list_lock, flags); | ||
1598 | } | ||
1339 | #endif | 1599 | #endif |