Lines Matching +full:console +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0-only
10 * to the console. Added hook for sending the console messages
11 * elsewhere, in preparation for a serial line console (someday).
26 #include <linux/console.h>
74 EXPORT_TRACEPOINT_SYMBOL_GPL(console);
84 * console_mutex protects console_list updates and console->flags updates.
86 * accessible via the console list.
91 * console_sem protects updates to console->seq
92 * and also provides serialization for console printing.
147 return -EINVAL; in __control_devkmsg()
167 return -EINVAL; in __control_devkmsg()
209 return -EINVAL; in devkmsg_sysctl_set_loglvl()
232 return -EINVAL; in devkmsg_sysctl_set_loglvl()
241 * console_list_lock - Lock the console list
243 * For console list or console->flags updates
253 * Detecting if this context is really in the read-side critical in console_list_lock()
265 * console_list_unlock - Unlock the console list
276 * console_srcu_read_lock - Register a new reader for the
277 * SRCU-protected console list
279 * Use for_each_console_srcu() to iterate the console list
291 * console_srcu_read_unlock - Unregister an old reader from
292 * the SRCU-protected console list
319 * because spindump/WARN/etc from under console ->lock will in __down_trylock_console_sem()
320 * deadlock in printk()->down_trylock_console_sem() otherwise. in __down_trylock_console_sem()
352 * keeping track if we have the console semaphore held. It's
355 * paths in the console code where we end up in places I want
356 * locked without the console semaphore held).
361 * Array of consoles built from command line options (console=)
368 static int preferred_console = -1;
372 /* Flag: console code may call schedule() */
385 * own meta-data (@info).
387 * Every record meta-data carries the timestamp in microseconds, as well as
389 * messages use LOG_KERN; userspace-injected messages always carry a matching
398 * pairs), to provide userspace with a machine-readable message context.
400 * Examples for well-defined, commonly used property names are:
406 * SUBSYSTEM=pci driver-core subsystem name
408 * Valid characters in property names are [a-zA-Z0-9.-_]. Property names
424 * userspace, it is a kernel-private implementation detail that might
435 * non-prinatable characters are escaped in the "\xff" notation.
444 * panic and risk deadlock on console resources.
483 * Define the average message size. This only affects the number of
492 _DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS,
500 * We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
502 * it's safe to access per-CPU data.
514 raw_write_seqcount_latch(&ls->latch); in latched_seq_write()
515 ls->val[0] = val; in latched_seq_write()
516 raw_write_seqcount_latch(&ls->latch); in latched_seq_write()
517 ls->val[1] = val; in latched_seq_write()
528 seq = raw_read_seqcount_latch(&ls->latch); in latched_seq_read_nolock()
530 val = ls->val[idx]; in latched_seq_read_nolock()
531 } while (raw_read_seqcount_latch_retry(&ls->latch, seq)); in latched_seq_read_nolock()
542 /* Return log buffer size */
570 *text_len -= *trunc_msg_len; in truncate_msg()
582 * Unless restricted, we allow "read all" and "get buffer size" in syslog_action_restricted()
609 current->comm, task_pid_nr(current)); in check_syslog_permissions()
612 return -EPERM; in check_syslog_permissions()
624 static ssize_t info_print_ext_header(char *buf, size_t size, in info_print_ext_header() argument
627 u64 ts_usec = info->ts_nsec; in info_print_ext_header()
630 u32 id = info->caller_id; in info_print_ext_header()
640 return scnprintf(buf, size, "%u,%llu,%llu,%c%s;", in info_print_ext_header()
641 (info->facility << 3) | info->level, info->seq, in info_print_ext_header()
642 ts_usec, info->flags & LOG_CONT ? 'c' : '-', caller); in info_print_ext_header()
645 static ssize_t msg_add_ext_text(char *buf, size_t size, in msg_add_ext_text() argument
649 char *p = buf, *e = buf + size; in msg_add_ext_text()
652 /* escape non-printable characters */ in msg_add_ext_text()
657 p += scnprintf(p, e - p, "\\x%02x", c); in msg_add_ext_text()
663 return p - buf; in msg_add_ext_text()
666 static ssize_t msg_add_dict_text(char *buf, size_t size, in msg_add_dict_text() argument
675 len = msg_add_ext_text(buf, size, "", 0, ' '); /* dict prefix */ in msg_add_dict_text()
676 len += msg_add_ext_text(buf + len, size - len, key, strlen(key), '='); in msg_add_dict_text()
677 len += msg_add_ext_text(buf + len, size - len, val, val_len, '\n'); in msg_add_dict_text()
682 static ssize_t msg_print_ext_body(char *buf, size_t size, in msg_print_ext_body() argument
688 len = msg_add_ext_text(buf, size, text, text_len, '\n'); in msg_print_ext_body()
693 len += msg_add_dict_text(buf + len, size - len, "SUBSYSTEM", in msg_print_ext_body()
694 dev_info->subsystem); in msg_print_ext_body()
695 len += msg_add_dict_text(buf + len, size - len, "DEVICE", in msg_print_ext_body()
696 dev_info->device); in msg_print_ext_body()
701 /* /dev/kmsg - userspace message inject/listen interface */
727 struct file *file = iocb->ki_filp; in devkmsg_write()
728 struct devkmsg_user *user = file->private_data; in devkmsg_write()
733 return -EINVAL; in devkmsg_write()
741 if (!___ratelimit(&user->rs, current->comm)) in devkmsg_write()
747 return -ENOMEM; in devkmsg_write()
752 return -EFAULT; in devkmsg_write()
756 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace in devkmsg_write()
762 * kernel-generated messages from userspace-injected ones. in devkmsg_write()
787 struct devkmsg_user *user = file->private_data; in devkmsg_read()
788 char *outbuf = &user->pbufs.outbuf[0]; in devkmsg_read()
790 .pbufs = &user->pbufs, in devkmsg_read()
794 ret = mutex_lock_interruptible(&user->lock); in devkmsg_read()
798 if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, false)) { in devkmsg_read()
799 if (file->f_flags & O_NONBLOCK) { in devkmsg_read()
800 ret = -EAGAIN; in devkmsg_read()
815 printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, in devkmsg_read()
823 atomic64_set(&user->seq, pmsg.seq); in devkmsg_read()
824 ret = -EPIPE; in devkmsg_read()
828 atomic64_set(&user->seq, pmsg.seq + 1); in devkmsg_read()
831 ret = -EINVAL; in devkmsg_read()
836 ret = -EFAULT; in devkmsg_read()
841 mutex_unlock(&user->lock); in devkmsg_read()
849 * entire variable length messages (records). Non-standard values are
855 struct devkmsg_user *user = file->private_data; in devkmsg_llseek()
859 return -ESPIPE; in devkmsg_llseek()
864 atomic64_set(&user->seq, prb_first_valid_seq(prb)); in devkmsg_llseek()
869 * like issued by 'dmesg -c'. Reading /dev/kmsg itself in devkmsg_llseek()
872 atomic64_set(&user->seq, latched_seq_read_nolock(&clear_seq)); in devkmsg_llseek()
876 atomic64_set(&user->seq, prb_next_seq(prb)); in devkmsg_llseek()
879 ret = -EINVAL; in devkmsg_llseek()
886 struct devkmsg_user *user = file->private_data; in devkmsg_poll()
892 if (prb_read_valid_info(prb, atomic64_read(&user->seq), &info, NULL)) { in devkmsg_poll()
894 if (info.seq != atomic64_read(&user->seq)) in devkmsg_poll()
909 return -EPERM; in devkmsg_open()
911 /* write-only does not need any file context */ in devkmsg_open()
912 if ((file->f_flags & O_ACCMODE) != O_WRONLY) { in devkmsg_open()
921 return -ENOMEM; in devkmsg_open()
923 ratelimit_default_init(&user->rs); in devkmsg_open()
924 ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE); in devkmsg_open()
926 mutex_init(&user->lock); in devkmsg_open()
928 atomic64_set(&user->seq, prb_first_valid_seq(prb)); in devkmsg_open()
930 file->private_data = user; in devkmsg_open()
936 struct devkmsg_user *user = file->private_data; in devkmsg_release()
938 ratelimit_state_exit(&user->rs); in devkmsg_release()
940 mutex_destroy(&user->lock); in devkmsg_release()
972 * Export struct size and field offsets. User space tools can in log_buf_vmcoreinfo_setup()
1005 VMCOREINFO_LENGTH(printk_info_subsystem, sizeof(dev_info->subsystem)); in log_buf_vmcoreinfo_setup()
1007 VMCOREINFO_LENGTH(printk_info_device, sizeof(dev_info->device)); in log_buf_vmcoreinfo_setup()
1027 static void __init log_buf_len_update(u64 size) in log_buf_len_update() argument
1029 if (size > (u64)LOG_BUF_LEN_MAX) { in log_buf_len_update()
1030 size = (u64)LOG_BUF_LEN_MAX; in log_buf_len_update()
1034 if (size) in log_buf_len_update()
1035 size = roundup_pow_of_two(size); in log_buf_len_update()
1036 if (size > log_buf_len) in log_buf_len_update()
1037 new_log_buf_len = (unsigned long)size; in log_buf_len_update()
1043 u64 size; in log_buf_len_setup() local
1046 return -EINVAL; in log_buf_len_setup()
1048 size = memparse(str, &str); in log_buf_len_setup()
1050 log_buf_len_update(size); in log_buf_len_setup()
1071 cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN; in log_buf_add_cpu()
1081 pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN); in log_buf_add_cpu()
1100 prb_rec_init_wr(&dest_r, r->info->text_len); in add_to_rb()
1105 memcpy(&dest_r.text_buf[0], &r->text_buf[0], r->info->text_len); in add_to_rb()
1106 dest_r.info->text_len = r->info->text_len; in add_to_rb()
1107 dest_r.info->facility = r->info->facility; in add_to_rb()
1108 dest_r.info->level = r->info->level; in add_to_rb()
1109 dest_r.info->flags = r->info->flags; in add_to_rb()
1110 dest_r.info->ts_nsec = r->info->ts_nsec; in add_to_rb()
1111 dest_r.info->caller_id = r->info->caller_id; in add_to_rb()
1112 memcpy(&dest_r.info->dev_info, &r->info->dev_info, sizeof(dest_r.info->dev_info)); in add_to_rb()
1137 * Some archs call setup_log_buf() multiple times - first is very in setup_log_buf()
1138 * early, e.g. from setup_arch(), and second - when percpu_areas in setup_log_buf()
1201 free -= text_size; in setup_log_buf()
1218 free -= text_size; in setup_log_buf()
1223 prb_next_seq(&printk_rb_static) - seq); in setup_log_buf()
1250 "ignore loglevel setting (prints all kernel messages to the console)");
1294 k--; in boot_delay_msec()
1347 len = print_syslog((info->facility << 3) | info->level, buf); in info_print_prefix()
1350 len += print_time(info->ts_nsec, buf + len); in info_print_prefix()
1352 len += print_caller(info->caller_id, buf + len); in info_print_prefix()
1367 * - Add prefix for each line.
1368 * - Drop truncated lines that no longer fit into the buffer.
1369 * - Add the trailing newline that has been removed in vprintk_store().
1370 * - Add a string terminator.
1373 * return value is @r->text_buf_size - 1;
1382 size_t text_len = r->info->text_len; in record_print_text()
1383 size_t buf_size = r->text_buf_size; in record_print_text()
1384 char *text = r->text_buf; in record_print_text()
1399 prefix_len = info_print_prefix(r->info, syslog, time, prefix); in record_print_text()
1405 * @len: number of bytes prepared in r->text_buf in record_print_text()
1410 line_len = next - text; in record_print_text()
1427 text_len = buf_size - len - prefix_len - 1 - 1; in record_print_text()
1466 text_len -= line_len + 1; in record_print_text()
1475 r->text_buf[len] = 0; in record_print_text()
1494 return ((prefix_len * line_count) + info->text_len + 1); in get_record_print_text_size()
1499 * records up to (but not including) @max_seq fit into @size.
1502 * does not require an upper bound, -1 can be used for @max_seq.
1504 static u64 find_first_fitting_seq(u64 start_seq, u64 max_seq, size_t size, in find_first_fitting_seq() argument
1512 /* Determine the size of the records up to @max_seq. */ in find_first_fitting_seq()
1533 if (len <= size || info.seq >= max_seq) in find_first_fitting_seq()
1535 len -= get_record_print_text_size(&info, line_count, syslog, time); in find_first_fitting_seq()
1541 /* The caller is responsible for making sure @size is greater than 0. */
1542 static int syslog_print(char __user *buf, int size) in syslog_print() argument
1552 return -ENOMEM; in syslog_print()
1596 if (r.info->seq != syslog_seq) { in syslog_print()
1598 syslog_seq = r.info->seq; in syslog_print()
1611 if (n - syslog_partial <= size) { in syslog_print()
1613 syslog_seq = r.info->seq + 1; in syslog_print()
1614 n -= syslog_partial; in syslog_print()
1618 n = size; in syslog_print()
1632 len = -EFAULT; in syslog_print()
1637 size -= n; in syslog_print()
1639 } while (size); in syslog_print()
1646 static int syslog_print_all(char __user *buf, int size, bool clear) in syslog_print_all() argument
1657 return -ENOMEM; in syslog_print_all()
1662 * into the user-provided buffer for this dump. in syslog_print_all()
1664 seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1, in syslog_print_all()
1665 size, true, time); in syslog_print_all()
1674 if (len + textlen > size) { in syslog_print_all()
1675 seq--; in syslog_print_all()
1680 len = -EFAULT; in syslog_print_all()
1723 return -EINVAL; in do_syslog()
1727 return -EFAULT; in do_syslog()
1737 return -EINVAL; in do_syslog()
1741 return -EFAULT; in do_syslog()
1748 /* Disable logging to console */ in do_syslog()
1754 /* Enable logging to console */ in do_syslog()
1761 /* Set level of messages printed to console */ in do_syslog()
1764 return -EINVAL; in do_syslog()
1768 /* Implicitly re-enable logging to console */ in do_syslog()
1786 * Short-cut for poll(/"proc/kmsg") which simply checks in do_syslog()
1787 * for pending data, not the size; return the count of in do_syslog()
1790 error = prb_next_seq(prb) - syslog_seq; in do_syslog()
1802 error -= syslog_partial; in do_syslog()
1806 /* Size of the log buffer */ in do_syslog()
1811 error = -EINVAL; in do_syslog()
1824 * Special console_lock variants that help to reduce the risk of soft-lockups.
1839 * console_lock_spinning_enable - mark beginning of code where another
1858 * console_lock_spinning_disable_and_check - mark end of code where another
1907 * console_trylock_spinning - try to get console_lock by busy waiting
1970 * The owner passed the console lock to us. in console_trylock_spinning()
1971 * Since we did not spin on console lock, annotate in console_trylock_spinning()
1982 * additional NMI context per CPU is also separately tracked. Until per-CPU
1995 * a WARN), but a higher value is used in case some printk-internal errors
2046 (*(recursion_ptr))--; \
2059 while (m--) { in printk_delay()
2073 * printk_parse_prefix - Parse level and control flags.
2103 *level = kern_level - '0'; in printk_parse_prefix()
2118 static u16 printk_sprint(char *text, u16 size, int facility, in printk_sprint() argument
2124 text_len = vscnprintf(text, size, fmt, args); in printk_sprint()
2127 if (text_len && text[text_len - 1] == '\n') { in printk_sprint()
2128 text_len--; in printk_sprint()
2138 text_len -= prefix_len; in printk_sprint()
2206 text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size, in vprintk_store()
2208 r.info->text_len += text_len; in vprintk_store()
2211 r.info->flags |= LOG_NEWLINE; in vprintk_store()
2241 r.info->text_len = text_len + trunc_msg_len; in vprintk_store()
2242 r.info->facility = facility; in vprintk_store()
2243 r.info->level = level & 7; in vprintk_store()
2244 r.info->flags = flags & 0x1f; in vprintk_store()
2245 r.info->ts_nsec = ts_nsec; in vprintk_store()
2246 r.info->caller_id = caller_id; in vprintk_store()
2248 memcpy(&r.info->dev_info, dev_info, sizeof(r.info->dev_info)); in vprintk_store()
2289 * The caller may be holding system-critical or in vprintk_emit()
2290 * timing-sensitive locks. Disable preemption during in vprintk_emit()
2297 * Try to acquire and then immediately release the console in vprintk_emit()
2336 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress);
2349 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; } in __pr_flush()
2354 struct console *early_console;
2369 early_console->write(early_console, buf, n); in early_printk()
2379 * @c console was defined by the user on the command line. in set_user_specified()
2382 c->user_specified = true; in set_user_specified()
2383 /* At least one console defined by the user on the command line. */ in set_user_specified()
2394 * We use a signed short index for struct console for device drivers to in __add_preferred_console()
2396 * value is not valid for preferred console. in __add_preferred_console()
2399 return -EINVAL; in __add_preferred_console()
2406 i < MAX_CMDLINECONSOLES && c->name[0]; in __add_preferred_console()
2408 if (strcmp(c->name, name) == 0 && c->index == idx) { in __add_preferred_console()
2416 return -E2BIG; in __add_preferred_console()
2419 strscpy(c->name, name, sizeof(c->name)); in __add_preferred_console()
2420 c->options = options; in __add_preferred_console()
2424 c->index = idx; in __add_preferred_console()
2439 * Set up a console. Called via do_early_param() in init/main.c
2440 * for each "console=" parameter in the boot command line.
2449 * console="" or console=null have been suggested as a way to in console_setup()
2450 * disable console output. Use ttynull that has been created in console_setup()
2466 strncpy(buf + 4, str, sizeof(buf) - 5); in console_setup()
2468 strncpy(buf, str, sizeof(buf) - 1); in console_setup()
2470 buf[sizeof(buf) - 1] = 0; in console_setup()
2489 __setup("console=", console_setup);
2492 * add_preferred_console - add a device to the list of preferred consoles.
2495 * @options: options for this console
2497 * The last preferred console added will be used for kernel messages
2499 * above to handle user-supplied console arguments; however it can also
2500 * be used by arch-specific code either to override the user or more
2501 * commonly to provide a default console (ie from PROM variables) when
2520 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2533 MODULE_PARM_DESC(console_no_auto_verbose, "Disable console loglevel raise to highest on oops/panic/…
2536 * suspend_console - suspend the console subsystem
2542 struct console *con; in suspend_console()
2546 pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); in suspend_console()
2551 console_srcu_write_flags(con, con->flags | CON_SUSPENDED); in suspend_console()
2565 struct console *con; in resume_console()
2572 console_srcu_write_flags(con, con->flags & ~CON_SUSPENDED); in resume_console()
2586 * console_cpu_notify - print deferred console messages after CPU hotplug
2590 * will be printed on the console only if there are CON_ANYTIME consoles.
2625 * console_lock - block the console subsystem from printing
2647 * console_trylock - try to block the console subsystem from printing
2674 * Check if the given console is currently capable and allowed to print
2679 static inline bool console_is_usable(struct console *con) in console_is_usable()
2689 if (!con->write) in console_is_usable()
2693 * Console drivers may assume that per-cpu resources have been in console_is_usable()
2712 * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". This
2720 * If the message text in @pmsg->pbufs->outbuf does not have enough space for
2723 * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
2727 struct printk_buffers *pbufs = pmsg->pbufs; in console_prepend_dropped()
2728 const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); in console_prepend_dropped()
2729 const size_t outbuf_sz = sizeof(pbufs->outbuf); in console_prepend_dropped()
2730 char *scratchbuf = &pbufs->scratchbuf[0]; in console_prepend_dropped()
2731 char *outbuf = &pbufs->outbuf[0]; in console_prepend_dropped()
2746 if (pmsg->outbuf_len + len >= outbuf_sz) { in console_prepend_dropped()
2748 pmsg->outbuf_len = outbuf_sz - (len + 1); in console_prepend_dropped()
2749 outbuf[pmsg->outbuf_len] = 0; in console_prepend_dropped()
2752 memmove(outbuf + len, outbuf, pmsg->outbuf_len + 1); in console_prepend_dropped()
2754 pmsg->outbuf_len += len; in console_prepend_dropped()
2761 * @pmsg will contain the formatted result. @pmsg->pbufs must point to a
2768 * console output.
2781 struct printk_buffers *pbufs = pmsg->pbufs; in printk_get_next_message()
2782 const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); in printk_get_next_message()
2783 const size_t outbuf_sz = sizeof(pbufs->outbuf); in printk_get_next_message()
2784 char *scratchbuf = &pbufs->scratchbuf[0]; in printk_get_next_message()
2785 char *outbuf = &pbufs->outbuf[0]; in printk_get_next_message()
2794 * Formatting normal messages is done in-place, so read the ringbuffer in printk_get_next_message()
2805 pmsg->seq = r.info->seq; in printk_get_next_message()
2806 pmsg->dropped = r.info->seq - seq; in printk_get_next_message()
2812 if (pmsg->dropped && in printk_get_next_message()
2816 …pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\… in printk_get_next_message()
2819 /* Skip record that has level above the console loglevel. */ in printk_get_next_message()
2820 if (may_suppress && suppress_message_printing(r.info->level)) in printk_get_next_message()
2825 len += msg_print_ext_body(outbuf + len, outbuf_sz - len, in printk_get_next_message()
2826 &r.text_buf[0], r.info->text_len, &r.info->dev_info); in printk_get_next_message()
2831 pmsg->outbuf_len = len; in printk_get_next_message()
2836 * Used as the printk buffers for non-panic, serialized console printing.
2843 * Print one record for the given console. The record printed is whatever
2844 * record is the next available record for the given console.
2852 * Returns false if the given console has no next record to print, otherwise
2857 static bool console_emit_next_record(struct console *con, bool *handover, int cookie) in console_emit_next_record()
2868 if (!printk_get_next_message(&pmsg, con->seq, is_extended, true)) in console_emit_next_record()
2871 con->dropped += pmsg.dropped; in console_emit_next_record()
2875 con->seq = pmsg.seq + 1; in console_emit_next_record()
2879 if (con->dropped && !is_extended) { in console_emit_next_record()
2880 console_prepend_dropped(&pmsg, con->dropped); in console_emit_next_record()
2881 con->dropped = 0; in console_emit_next_record()
2901 con->write(con, outbuf, pmsg.outbuf_len); in console_emit_next_record()
2905 con->seq = pmsg.seq + 1; in console_emit_next_record()
2915 static bool console_emit_next_record(struct console *con, bool *handover, int cookie) in console_emit_next_record()
2937 * Returns true when there was at least one usable console and all messages
2949 struct console *con; in console_flush_all()
2977 if (con->seq > *next_seq) in console_flush_all()
2978 *next_seq = con->seq; in console_flush_all()
3002 * console_unlock - unblock the console subsystem from printing
3005 * the console subsystem.
3007 * While the console_lock was held, console output may have been buffered
3021 * Console drivers are called with interrupts disabled, so in console_unlock()
3024 * console registration path, and should invoke cond_resched() in console_unlock()
3026 * scheduling stall on a slow console leading to RCU stall and in console_unlock()
3051 * console_flush_all() but before unlocking the console. in console_unlock()
3052 * Re-check if there is a new record to flush. If the trylock in console_unlock()
3060 * console_conditional_schedule - yield the CPU if required
3062 * If the console code is currently allowed to sleep, and
3078 struct console *c; in console_unblank()
3084 * console lock, which in particular can be dangerous if in console_unblank()
3089 if ((console_srcu_read_flags(c) & CON_ENABLED) && c->unblank) { in console_unblank()
3099 * Stop console printing because the unblank() callback may in console_unblank()
3100 * assume the console is not within its write() callback. in console_unblank()
3103 * In that case, attempt a trylock as best-effort. in console_unblank()
3106 /* Semaphores are not NMI-safe. */ in console_unblank()
3111 * Attempting to trylock the console lock can deadlock in console_unblank()
3126 if ((console_srcu_read_flags(c) & CON_ENABLED) && c->unblank) in console_unblank()
3127 c->unblank(); in console_unblank()
3138 * console_flush_on_panic - flush console content on panic
3149 * Ignore the console lock and flush out the messages. Attempting a in console_flush_on_panic()
3152 * - if it is contended, it must be ignored anyway in console_flush_on_panic()
3153 * - console_lock() and console_trylock() block and fail in console_flush_on_panic()
3154 * respectively in panic for non-panic CPUs in console_flush_on_panic()
3155 * - semaphores are not NMI-safe in console_flush_on_panic()
3159 * If another context is holding the console lock, in console_flush_on_panic()
3166 struct console *c; in console_flush_on_panic()
3184 c->seq = seq; in console_flush_on_panic()
3194 * Return the console tty driver structure and its associated index
3198 struct console *c; in console_device()
3204 * other console operations. For example, fg_console is in console_device()
3211 if (!c->device) in console_device()
3213 driver = c->device(c, index); in console_device()
3224 * Prevent further output on the passed console device so that (for example)
3225 * serial drivers can disable console output before suspending a port, and can
3226 * re-enable output afterwards.
3228 void console_stop(struct console *console) in console_stop() argument
3230 __pr_flush(console, 1000, true); in console_stop()
3232 console_srcu_write_flags(console, console->flags & ~CON_ENABLED); in console_stop()
3237 * be able to see that this console is disabled so that (for example) in console_stop()
3245 void console_start(struct console *console) in console_start() argument
3248 console_srcu_write_flags(console, console->flags | CON_ENABLED); in console_start()
3250 __pr_flush(console, 1000, true); in console_start()
3259 pr_info("debug: skip boot console de-registration.\n"); in keep_bootcon_setup()
3268 * the newly registered console with any of the ones selected
3275 static int try_enable_preferred_console(struct console *newcon, in try_enable_preferred_console()
3282 i < MAX_CMDLINECONSOLES && c->name[0]; in try_enable_preferred_console()
3284 if (c->user_specified != user_specified) in try_enable_preferred_console()
3286 if (!newcon->match || in try_enable_preferred_console()
3287 newcon->match(newcon, c->name, c->index, c->options) != 0) { in try_enable_preferred_console()
3289 BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); in try_enable_preferred_console()
3290 if (strcmp(c->name, newcon->name) != 0) in try_enable_preferred_console()
3292 if (newcon->index >= 0 && in try_enable_preferred_console()
3293 newcon->index != c->index) in try_enable_preferred_console()
3295 if (newcon->index < 0) in try_enable_preferred_console()
3296 newcon->index = c->index; in try_enable_preferred_console()
3301 if (newcon->setup && in try_enable_preferred_console()
3302 (err = newcon->setup(newcon, c->options)) != 0) in try_enable_preferred_console()
3305 newcon->flags |= CON_ENABLED; in try_enable_preferred_console()
3307 newcon->flags |= CON_CONSDEV; in try_enable_preferred_console()
3313 * without matching. Accept the pre-enabled consoles only when match() in try_enable_preferred_console()
3316 if (newcon->flags & CON_ENABLED && c->user_specified == user_specified) in try_enable_preferred_console()
3319 return -ENOENT; in try_enable_preferred_console()
3322 /* Try to enable the console unconditionally */
3323 static void try_enable_default_console(struct console *newcon) in try_enable_default_console()
3325 if (newcon->index < 0) in try_enable_default_console()
3326 newcon->index = 0; in try_enable_default_console()
3328 if (newcon->setup && newcon->setup(newcon, NULL) != 0) in try_enable_default_console()
3331 newcon->flags |= CON_ENABLED; in try_enable_default_console()
3333 if (newcon->device) in try_enable_default_console()
3334 newcon->flags |= CON_CONSDEV; in try_enable_default_console()
3337 static void console_init_seq(struct console *newcon, bool bootcon_registered) in console_init_seq()
3339 struct console *con; in console_init_seq()
3342 if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) { in console_init_seq()
3345 newcon->seq = syslog_seq; in console_init_seq()
3349 newcon->seq = prb_next_seq(prb); in console_init_seq()
3354 * device as @newcon. Since it is not known which boot console in console_init_seq()
3356 * start with the message of the enabled boot console that is in console_init_seq()
3361 * Hold the console_lock to stop console printing and in console_init_seq()
3362 * guarantee safe access to console->seq. in console_init_seq()
3367 * Flush all consoles and set the console to start at in console_init_seq()
3370 if (!console_flush_all(true, &newcon->seq, &handover)) { in console_init_seq()
3383 newcon->seq = prb_next_seq(prb); in console_init_seq()
3385 if ((con->flags & CON_BOOT) && in console_init_seq()
3386 (con->flags & CON_ENABLED) && in console_init_seq()
3387 con->seq < newcon->seq) { in console_init_seq()
3388 newcon->seq = con->seq; in console_init_seq()
3399 hlist_entry(console_list.first, struct console, node)
3401 static int unregister_console_locked(struct console *console);
3404 * The console driver calls this routine during kernel initialization
3405 * to register the console printing procedure with printk() and to
3407 * console driver was initialized.
3410 * early_printk) - sometimes before setup_arch() completes - be careful
3411 * of what kernel features are used - they may not be initialised yet.
3413 * There are two types of consoles - bootconsoles (early_printk) and
3416 * - Any number of bootconsoles can be registered at any time.
3417 * - As soon as a "real" console is registered, all bootconsoles
3419 * - Once a "real" console is registered, any attempt to register a
3422 void register_console(struct console *newcon) in register_console()
3424 struct console *con; in register_console()
3432 if (WARN(con == newcon, "console '%s%d' already registered\n", in register_console()
3433 con->name, con->index)) { in register_console()
3437 if (con->flags & CON_BOOT) in register_console()
3444 if ((newcon->flags & CON_BOOT) && realcon_registered) { in register_console()
3446 newcon->name, newcon->index); in register_console()
3450 if (newcon->flags & CON_NBCON) { in register_console()
3452 * Ensure the nbcon console buffers can be allocated in register_console()
3460 * See if we want to enable this console driver by default. in register_console()
3462 * Nope when a console is preferred by the command line, device in register_console()
3465 * The first real console with tty binding (driver) wins. More in register_console()
3468 * Note that a console with tty binding will have CON_CONSDEV in register_console()
3472 if (hlist_empty(&console_list) || !console_first()->device || in register_console()
3473 console_first()->flags & CON_BOOT) { in register_console()
3478 /* See if this console matches one we selected on the command line */ in register_console()
3482 if (err == -ENOENT) in register_console()
3485 /* printk() messages are not printed to the Braille console. */ in register_console()
3486 if (err || newcon->flags & CON_BRL) { in register_console()
3487 if (newcon->flags & CON_NBCON) in register_console()
3493 * If we have a bootconsole, and are switching to a real console, in register_console()
3494 * don't print everything out again, since when the boot console, and in register_console()
3495 * the real console are the same physical device, it's annoying to in register_console()
3499 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) { in register_console()
3500 newcon->flags &= ~CON_PRINTBUFFER; in register_console()
3503 newcon->dropped = 0; in register_console()
3506 if (newcon->flags & CON_NBCON) in register_console()
3510 * Put this console in the list - keep the in register_console()
3515 newcon->flags |= CON_CONSDEV; in register_console()
3516 hlist_add_head_rcu(&newcon->node, &console_list); in register_console()
3518 } else if (newcon->flags & CON_CONSDEV) { in register_console()
3520 console_srcu_write_flags(console_first(), console_first()->flags & ~CON_CONSDEV); in register_console()
3521 hlist_add_head_rcu(&newcon->node, &console_list); in register_console()
3524 hlist_add_behind_rcu(&newcon->node, console_list.first); in register_console()
3529 * on all contexts being able to see the new console before in register_console()
3536 * By unregistering the bootconsoles after we enable the real console in register_console()
3537 * we get the "console xxx enabled" message on all the consoles - in register_console()
3538 * boot consoles, real consoles, etc - this is to ensure that end in register_console()
3540 * went to the bootconsole (that they do not see on the real console) in register_console()
3544 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && in register_console()
3549 if (con->flags & CON_BOOT) in register_console()
3559 static int unregister_console_locked(struct console *console) in unregister_console_locked() argument
3565 con_printk(KERN_INFO, console, "disabled\n"); in unregister_console_locked()
3567 res = _braille_unregister_console(console); in unregister_console_locked()
3574 console_srcu_write_flags(console, console->flags & ~CON_ENABLED); in unregister_console_locked()
3576 if (!console_is_registered_locked(console)) in unregister_console_locked()
3577 return -ENODEV; in unregister_console_locked()
3579 hlist_del_init_rcu(&console->node); in unregister_console_locked()
3583 * If this isn't the last console and it has CON_CONSDEV set, we in unregister_console_locked()
3584 * need to set it on the next preferred console. in unregister_console_locked()
3588 * console has any device attached. Oh well.... in unregister_console_locked()
3590 if (!hlist_empty(&console_list) && console->flags & CON_CONSDEV) in unregister_console_locked()
3591 console_srcu_write_flags(console_first(), console_first()->flags | CON_CONSDEV); in unregister_console_locked()
3595 * must not be able to see this console in the list so that any in unregister_console_locked()
3600 if (console->flags & CON_NBCON) in unregister_console_locked()
3601 nbcon_free(console); in unregister_console_locked()
3605 if (console->exit) in unregister_console_locked()
3606 res = console->exit(console); in unregister_console_locked()
3611 int unregister_console(struct console *console) in unregister_console() argument
3616 res = unregister_console_locked(console); in unregister_console()
3623 * console_force_preferred_locked - force a registered console preferred
3624 * @con: The registered console to force preferred.
3628 void console_force_preferred_locked(struct console *con) in console_force_preferred_locked()
3630 struct console *cur_pref_con; in console_force_preferred_locked()
3642 * Delete, but do not re-initialize the entry. This allows the console in console_force_preferred_locked()
3644 * checks), even though it was briefly removed from the console list. in console_force_preferred_locked()
3646 hlist_del_rcu(&con->node); in console_force_preferred_locked()
3649 * Ensure that all SRCU list walks have completed so that the console in console_force_preferred_locked()
3650 * can be added to the beginning of the console list and its forward in console_force_preferred_locked()
3651 * list pointer can be re-initialized. in console_force_preferred_locked()
3655 con->flags |= CON_CONSDEV; in console_force_preferred_locked()
3656 WARN_ON(!con->device); in console_force_preferred_locked()
3659 console_srcu_write_flags(cur_pref_con, cur_pref_con->flags & ~CON_CONSDEV); in console_force_preferred_locked()
3660 hlist_add_head_rcu(&con->node, &console_list); in console_force_preferred_locked()
3665 * Initialize the console device. This is called *early*, so
3680 * set up the console device so that later boot sequences can in console_init()
3684 trace_initcall_level("console"); in console_init()
3700 * module, the real console hasn't registered yet at this point, there will
3701 * be a brief interval in which no messages are logged to the console, which
3706 * get unregistered when the real preferred console is registered.
3711 struct console *con; in printk_late_init()
3716 if (!(con->flags & CON_BOOT)) in printk_late_init()
3721 init_section_contains(con->write, 0) || in printk_late_init()
3722 init_section_contains(con->read, 0) || in printk_late_init()
3723 init_section_contains(con->device, 0) || in printk_late_init()
3724 init_section_contains(con->unblank, 0) || in printk_late_init()
3725 init_section_contains(con->data, 0)) { in printk_late_init()
3731 con->name, con->index); in printk_late_init()
3749 /* If @con is specified, only wait for that console. Otherwise wait for all. */
3750 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) in __pr_flush()
3754 struct console *c; in __pr_flush()
3778 * console->seq. Releasing console_lock flushes more in __pr_flush()
3802 printk_seq = c->seq; in __pr_flush()
3806 diff += seq - printk_seq; in __pr_flush()
3822 slept_jiffies = jiffies - begin_jiffies; in __pr_flush()
3824 remaining_jiffies -= min(slept_jiffies, remaining_jiffies); in __pr_flush()
3833 * pr_flush() - Wait for printing threads to catch up.
3838 * A value of 0 for @timeout_ms means no waiting will occur. A value of -1
3844 * Context: Process context. May sleep while acquiring console lock.
3853 * Delayed printk version, for scheduler-internal messages:
3903 * wake_up_klogd - Wake kernel logging daemon
3906 * and the console printing of those records has already occurred or is
3918 * defer_console_output - Wake kernel logging daemon and trigger
3919 * console printing in a deferred context
3922 * this context is responsible for console printing those records, but
3923 * the current context is not allowed to perform the console printing.
3924 * Trigger an irq_work context to perform the console printing. This
3964 * every 5s to make a denial-of-service attack impossible.
3975 * printk_timed_ratelimit - caller-controlled printk ratelimiting
3986 unsigned long elapsed = jiffies - *caller_jiffies; in printk_timed_ratelimit()
4000 * kmsg_dump_register - register a kernel log dumper.
4005 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
4010 int err = -EBUSY; in kmsg_dump_register()
4013 if (!dumper->dump) in kmsg_dump_register()
4014 return -EINVAL; in kmsg_dump_register()
4018 if (!dumper->registered) { in kmsg_dump_register()
4019 dumper->registered = 1; in kmsg_dump_register()
4020 list_add_tail_rcu(&dumper->list, &dump_list); in kmsg_dump_register()
4030 * kmsg_dump_unregister - unregister a kmsg dumper.
4034 * %-EINVAL otherwise.
4039 int err = -EINVAL; in kmsg_dump_unregister()
4042 if (dumper->registered) { in kmsg_dump_unregister()
4043 dumper->registered = 0; in kmsg_dump_unregister()
4044 list_del_rcu(&dumper->list); in kmsg_dump_unregister()
4075 * kmsg_dump - dump kernel log to kernel message dumpers.
4088 enum kmsg_dump_reason max_reason = dumper->max_reason; in kmsg_dump()
4102 dumper->dump(dumper, reason); in kmsg_dump()
4108 * kmsg_dump_get_line - retrieve one kmsg log line
4112 * @size: maximum size of the buffer
4125 char *line, size_t size, size_t *len) in kmsg_dump_get_line() argument
4134 if (iter->cur_seq < min_seq) in kmsg_dump_get_line()
4135 iter->cur_seq = min_seq; in kmsg_dump_get_line()
4137 prb_rec_init_rd(&r, &info, line, size); in kmsg_dump_get_line()
4141 if (!prb_read_valid(prb, iter->cur_seq, &r)) in kmsg_dump_get_line()
4145 if (!prb_read_valid_info(prb, iter->cur_seq, in kmsg_dump_get_line()
4154 iter->cur_seq = r.info->seq + 1; in kmsg_dump_get_line()
4164 * kmsg_dump_get_buffer - copy kmsg log lines
4168 * @size: maximum size of the buffer
4183 char *buf, size_t size, size_t *len_out) in kmsg_dump_get_buffer() argument
4194 if (!buf || !size) in kmsg_dump_get_buffer()
4197 if (iter->cur_seq < min_seq) in kmsg_dump_get_buffer()
4198 iter->cur_seq = min_seq; in kmsg_dump_get_buffer()
4200 if (prb_read_valid_info(prb, iter->cur_seq, &info, NULL)) { in kmsg_dump_get_buffer()
4201 if (info.seq != iter->cur_seq) { in kmsg_dump_get_buffer()
4203 iter->cur_seq = info.seq; in kmsg_dump_get_buffer()
4208 if (iter->cur_seq >= iter->next_seq) in kmsg_dump_get_buffer()
4213 * into the user-provided buffer for this dump. Pass in size-1 in kmsg_dump_get_buffer()
4215 * not write more than size-1 bytes of text into @buf. in kmsg_dump_get_buffer()
4217 seq = find_first_fitting_seq(iter->cur_seq, iter->next_seq, in kmsg_dump_get_buffer()
4218 size - 1, syslog, time); in kmsg_dump_get_buffer()
4226 prb_rec_init_rd(&r, &info, buf, size); in kmsg_dump_get_buffer()
4229 if (r.info->seq >= iter->next_seq) in kmsg_dump_get_buffer()
4235 prb_rec_init_rd(&r, &info, buf + len, size - len); in kmsg_dump_get_buffer()
4238 iter->next_seq = next_seq; in kmsg_dump_get_buffer()
4248 * kmsg_dump_rewind - reset the iterator
4257 iter->cur_seq = latched_seq_read_nolock(&clear_seq); in kmsg_dump_rewind()
4258 iter->next_seq = prb_next_seq(prb); in kmsg_dump_rewind()
4265 static atomic_t printk_cpu_sync_owner = ATOMIC_INIT(-1);
4269 * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant
4278 } while (atomic_read(&printk_cpu_sync_owner) != -1); in __printk_cpu_sync_wait()
4283 * __printk_cpu_sync_try_get() - Try to acquire the printk cpu-reentrant
4319 old = atomic_cmpxchg_acquire(&printk_cpu_sync_owner, -1, in __printk_cpu_sync_try_get()
4321 if (old == -1) { in __printk_cpu_sync_try_get()
4339 * __printk_cpu_sync_put() - Release the printk cpu-reentrant spinning lock.
4376 -1); /* LMM(__printk_cpu_sync_put:B) */ in __printk_cpu_sync_put()