Lines Matching +full:per +full:- +full:console
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>
75 EXPORT_TRACEPOINT_SYMBOL_GPL(console);
85 * console_mutex protects console_list updates and console->flags updates.
87 * accessible via the console list.
92 * console_sem protects updates to console->seq
93 * and also provides serialization for console printing.
148 return -EINVAL; in __control_devkmsg()
168 return -EINVAL; in __control_devkmsg()
210 return -EINVAL; in devkmsg_sysctl_set_loglvl()
233 return -EINVAL; in devkmsg_sysctl_set_loglvl()
242 * console_list_lock - Lock the console list
244 * For console list or console->flags updates
254 * Detecting if this context is really in the read-side critical in console_list_lock()
266 * console_list_unlock - Unlock the console list
277 * console_srcu_read_lock - Register a new reader for the
278 * SRCU-protected console list
280 * Use for_each_console_srcu() to iterate the console list
293 * console_srcu_read_unlock - Unregister an old reader from
294 * the SRCU-protected console list
322 * because spindump/WARN/etc from under console ->lock will in __down_trylock_console_sem()
323 * deadlock in printk()->down_trylock_console_sem() otherwise. in __down_trylock_console_sem()
378 * keeping track if we have the console semaphore held. It's
381 * paths in the console code where we end up in places I want
382 * locked without the console semaphore held).
387 * Array of consoles built from command line options (console=)
394 static int preferred_console = -1;
398 /* Flag: console code may call schedule() */
411 * own meta-data (@info).
413 * Every record meta-data carries the timestamp in microseconds, as well as
415 * messages use LOG_KERN; userspace-injected messages always carry a matching
424 * pairs), to provide userspace with a machine-readable message context.
426 * Examples for well-defined, commonly used property names are:
432 * SUBSYSTEM=pci driver-core subsystem name
434 * Valid characters in property names are [a-zA-Z0-9.-_]. Property names
450 * userspace, it is a kernel-private implementation detail that might
461 * non-prinatable characters are escaped in the "\xff" notation.
468 * Specifies if a legacy console is registered. If legacy consoles are
469 * present, it is necessary to perform the console lock/unlock dance
470 * whenever console flushing should occur.
475 * Specifies if an nbcon console is registered. If nbcon consoles are present,
482 * Specifies if a boot console is registered. If boot consoles are present,
484 * the console lock. This is because boot consoles and nbcon consoles may
541 _DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS,
549 * We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
551 * it's safe to access per-CPU data.
563 write_seqcount_latch_begin(&ls->latch); in latched_seq_write()
564 ls->val[0] = val; in latched_seq_write()
565 write_seqcount_latch(&ls->latch); in latched_seq_write()
566 ls->val[1] = val; in latched_seq_write()
567 write_seqcount_latch_end(&ls->latch); in latched_seq_write()
578 seq = read_seqcount_latch(&ls->latch); in latched_seq_read_nolock()
580 val = ls->val[idx]; in latched_seq_read_nolock()
581 } while (read_seqcount_latch_retry(&ls->latch, seq)); in latched_seq_read_nolock()
620 *text_len -= *trunc_msg_len; in truncate_msg()
651 return -EPERM; in check_syslog_permissions()
666 u64 ts_usec = info->ts_nsec; in info_print_ext_header()
669 u32 id = info->caller_id; in info_print_ext_header()
680 (info->facility << 3) | info->level, info->seq, in info_print_ext_header()
681 ts_usec, info->flags & LOG_CONT ? 'c' : '-', caller); in info_print_ext_header()
691 /* escape non-printable characters */ in msg_add_ext_text()
696 p += scnprintf(p, e - p, "\\x%02x", c); in msg_add_ext_text()
702 return p - buf; in msg_add_ext_text()
715 len += msg_add_ext_text(buf + len, size - len, key, strlen(key), '='); in msg_add_dict_text()
716 len += msg_add_ext_text(buf + len, size - len, val, val_len, '\n'); in msg_add_dict_text()
732 len += msg_add_dict_text(buf + len, size - len, "SUBSYSTEM", in msg_print_ext_body()
733 dev_info->subsystem); in msg_print_ext_body()
734 len += msg_add_dict_text(buf + len, size - len, "DEVICE", in msg_print_ext_body()
735 dev_info->device); in msg_print_ext_body()
740 /* /dev/kmsg - userspace message inject/listen interface */
766 struct file *file = iocb->ki_filp; in devkmsg_write()
767 struct devkmsg_user *user = file->private_data; in devkmsg_write()
772 return -EINVAL; in devkmsg_write()
780 if (!___ratelimit(&user->rs, current->comm)) in devkmsg_write()
786 return -ENOMEM; in devkmsg_write()
791 return -EFAULT; in devkmsg_write()
795 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace in devkmsg_write()
801 * kernel-generated messages from userspace-injected ones. in devkmsg_write()
826 struct devkmsg_user *user = file->private_data; in devkmsg_read()
827 char *outbuf = &user->pbufs.outbuf[0]; in devkmsg_read()
829 .pbufs = &user->pbufs, in devkmsg_read()
833 ret = mutex_lock_interruptible(&user->lock); in devkmsg_read()
837 if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, false)) { in devkmsg_read()
838 if (file->f_flags & O_NONBLOCK) { in devkmsg_read()
839 ret = -EAGAIN; in devkmsg_read()
854 printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, in devkmsg_read()
862 atomic64_set(&user->seq, pmsg.seq); in devkmsg_read()
863 ret = -EPIPE; in devkmsg_read()
867 atomic64_set(&user->seq, pmsg.seq + 1); in devkmsg_read()
870 ret = -EINVAL; in devkmsg_read()
875 ret = -EFAULT; in devkmsg_read()
880 mutex_unlock(&user->lock); in devkmsg_read()
888 * entire variable length messages (records). Non-standard values are
894 struct devkmsg_user *user = file->private_data; in devkmsg_llseek()
898 return -ESPIPE; in devkmsg_llseek()
903 atomic64_set(&user->seq, prb_first_valid_seq(prb)); in devkmsg_llseek()
908 * like issued by 'dmesg -c'. Reading /dev/kmsg itself in devkmsg_llseek()
911 atomic64_set(&user->seq, latched_seq_read_nolock(&clear_seq)); in devkmsg_llseek()
915 atomic64_set(&user->seq, prb_next_seq(prb)); in devkmsg_llseek()
918 ret = -EINVAL; in devkmsg_llseek()
925 struct devkmsg_user *user = file->private_data; in devkmsg_poll()
931 if (prb_read_valid_info(prb, atomic64_read(&user->seq), &info, NULL)) { in devkmsg_poll()
933 if (info.seq != atomic64_read(&user->seq)) in devkmsg_poll()
948 return -EPERM; in devkmsg_open()
950 /* write-only does not need any file context */ in devkmsg_open()
951 if ((file->f_flags & O_ACCMODE) != O_WRONLY) { in devkmsg_open()
960 return -ENOMEM; in devkmsg_open()
962 ratelimit_default_init(&user->rs); in devkmsg_open()
963 ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE); in devkmsg_open()
965 mutex_init(&user->lock); in devkmsg_open()
967 atomic64_set(&user->seq, prb_first_valid_seq(prb)); in devkmsg_open()
969 file->private_data = user; in devkmsg_open()
975 struct devkmsg_user *user = file->private_data; in devkmsg_release()
977 ratelimit_state_exit(&user->rs); in devkmsg_release()
979 mutex_destroy(&user->lock); in devkmsg_release()
1044 VMCOREINFO_LENGTH(printk_info_subsystem, sizeof(dev_info->subsystem)); in log_buf_vmcoreinfo_setup()
1046 VMCOREINFO_LENGTH(printk_info_device, sizeof(dev_info->device)); in log_buf_vmcoreinfo_setup()
1085 return -EINVAL; in log_buf_len_setup()
1110 cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN; in log_buf_add_cpu()
1139 prb_rec_init_wr(&dest_r, r->info->text_len); in add_to_rb()
1144 memcpy(&dest_r.text_buf[0], &r->text_buf[0], r->info->text_len); in add_to_rb()
1145 dest_r.info->text_len = r->info->text_len; in add_to_rb()
1146 dest_r.info->facility = r->info->facility; in add_to_rb()
1147 dest_r.info->level = r->info->level; in add_to_rb()
1148 dest_r.info->flags = r->info->flags; in add_to_rb()
1149 dest_r.info->ts_nsec = r->info->ts_nsec; in add_to_rb()
1150 dest_r.info->caller_id = r->info->caller_id; in add_to_rb()
1151 memcpy(&dest_r.info->dev_info, &r->info->dev_info, sizeof(dest_r.info->dev_info)); in add_to_rb()
1187 * Some archs call setup_log_buf() multiple times - first is very in setup_log_buf()
1188 * early, e.g. from setup_arch(), and second - when percpu_areas in setup_log_buf()
1256 free -= text_size; in setup_log_buf()
1273 free -= text_size; in setup_log_buf()
1278 prb_next_seq(&printk_rb_static) - seq); in setup_log_buf()
1307 "ignore loglevel setting (prints all kernel messages to the console)");
1351 k--; in boot_delay_msec()
1404 len = print_syslog((info->facility << 3) | info->level, buf); in info_print_prefix()
1407 len += print_time(info->ts_nsec, buf + len); in info_print_prefix()
1409 len += print_caller(info->caller_id, buf + len); in info_print_prefix()
1424 * - Add prefix for each line.
1425 * - Drop truncated lines that no longer fit into the buffer.
1426 * - Add the trailing newline that has been removed in vprintk_store().
1427 * - Add a string terminator.
1430 * return value is @r->text_buf_size - 1;
1439 size_t text_len = r->info->text_len; in record_print_text()
1440 size_t buf_size = r->text_buf_size; in record_print_text()
1441 char *text = r->text_buf; in record_print_text()
1456 prefix_len = info_print_prefix(r->info, syslog, time, prefix); in record_print_text()
1462 * @len: number of bytes prepared in r->text_buf in record_print_text()
1467 line_len = next - text; in record_print_text()
1484 text_len = buf_size - len - prefix_len - 1 - 1; in record_print_text()
1523 text_len -= line_len + 1; in record_print_text()
1532 r->text_buf[len] = 0; in record_print_text()
1551 return ((prefix_len * line_count) + info->text_len + 1); in get_record_print_text_size()
1559 * does not require an upper bound, -1 can be used for @max_seq.
1592 len -= get_record_print_text_size(&info, line_count, syslog, time); in find_first_fitting_seq()
1609 return -ENOMEM; in syslog_print()
1653 if (r.info->seq != syslog_seq) { in syslog_print()
1655 syslog_seq = r.info->seq; in syslog_print()
1668 if (n - syslog_partial <= size) { in syslog_print()
1670 syslog_seq = r.info->seq + 1; in syslog_print()
1671 n -= syslog_partial; in syslog_print()
1689 len = -EFAULT; in syslog_print()
1694 size -= n; in syslog_print()
1714 return -ENOMEM; in syslog_print_all()
1719 * into the user-provided buffer for this dump. in syslog_print_all()
1721 seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1, in syslog_print_all()
1732 seq--; in syslog_print_all()
1737 len = -EFAULT; in syslog_print_all()
1780 return -EINVAL; in do_syslog()
1784 return -EFAULT; in do_syslog()
1794 return -EINVAL; in do_syslog()
1798 return -EFAULT; in do_syslog()
1805 /* Disable logging to console */ in do_syslog()
1811 /* Enable logging to console */ in do_syslog()
1818 /* Set level of messages printed to console */ in do_syslog()
1821 return -EINVAL; in do_syslog()
1825 /* Implicitly re-enable logging to console */ in do_syslog()
1843 * Short-cut for poll(/"proc/kmsg") which simply checks in do_syslog()
1847 error = prb_next_seq(prb) - syslog_seq; in do_syslog()
1859 error -= syslog_partial; in do_syslog()
1868 error = -EINVAL; in do_syslog()
1881 * Special console_lock variants that help to reduce the risk of soft-lockups.
1896 * console_lock_spinning_enable - mark beginning of code where another
1908 * Non-panic CPUs abandon the flush anyway. in console_lock_spinning_enable()
1910 * Just keep the lockdep annotation. The panic-CPU should avoid in console_lock_spinning_enable()
1928 * console_lock_spinning_disable_and_check - mark end of code where another
1993 * console_trylock_spinning - try to get console_lock by busy waiting
2056 * The owner passed the console lock to us. in console_trylock_spinning()
2057 * Since we did not spin on console lock, annotate in console_trylock_spinning()
2074 * additional NMI context per CPU is also separately tracked. Until per-CPU
2087 * a WARN), but a higher value is used in case some printk-internal errors
2138 (*(recursion_ptr))--; \
2151 while (m--) { in printk_delay()
2165 * printk_parse_prefix - Parse level and control flags.
2195 *level = kern_level - '0'; in printk_parse_prefix()
2219 if (text_len && text[text_len - 1] == '\n') { in printk_sprint()
2220 text_len--; in printk_sprint()
2230 text_len -= prefix_len; in printk_sprint()
2301 text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size, in vprintk_store()
2303 r.info->text_len += text_len; in vprintk_store()
2306 r.info->flags |= LOG_FORCE_CON; in vprintk_store()
2309 r.info->flags |= LOG_NEWLINE; in vprintk_store()
2339 r.info->text_len = text_len + trunc_msg_len; in vprintk_store()
2340 r.info->facility = facility; in vprintk_store()
2341 r.info->level = level & 7; in vprintk_store()
2342 r.info->flags = flags & 0x1f; in vprintk_store()
2343 r.info->ts_nsec = ts_nsec; in vprintk_store()
2344 r.info->caller_id = caller_id; in vprintk_store()
2346 memcpy(&r.info->dev_info, dev_info, sizeof(r.info->dev_info)); in vprintk_store()
2361 * This acts as a one-way switch to allow legacy consoles to print from
2384 pr_info("allow messages from non-panic CPUs in panic()\n"); in debug_non_panic_cpus_setup()
2391 "allow messages from non-panic CPUs in panic()");
2407 * non-panic CPUs are generating any messages, they will be in vprintk_emit()
2436 * The caller may be holding system-critical or in vprintk_emit()
2437 * timing-sensitive locks. Disable preemption during in vprintk_emit()
2444 * Try to acquire and then immediately release the console in vprintk_emit()
2482 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress);
2494 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; } in __pr_flush()
2499 struct console *early_console;
2514 early_console->write(early_console, buf, n); in early_printk()
2524 * @c console was defined by the user on the command line. in set_user_specified()
2527 c->user_specified = true; in set_user_specified()
2528 /* At least one console defined by the user on the command line. */ in set_user_specified()
2540 return -EINVAL; in __add_preferred_console()
2543 * We use a signed short index for struct console for device drivers to in __add_preferred_console()
2545 * value is not valid when the console name and index are defined on in __add_preferred_console()
2549 return -EINVAL; in __add_preferred_console()
2556 i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]); in __add_preferred_console()
2558 if ((name && strcmp(c->name, name) == 0 && c->index == idx) || in __add_preferred_console()
2559 (devname && strcmp(c->devname, devname) == 0)) { in __add_preferred_console()
2567 return -E2BIG; in __add_preferred_console()
2571 strscpy(c->name, name); in __add_preferred_console()
2573 strscpy(c->devname, devname); in __add_preferred_console()
2574 c->options = options; in __add_preferred_console()
2578 c->index = idx; in __add_preferred_console()
2593 * Set up a console. Called via do_early_param() in init/main.c
2594 * for each "console=" parameter in the boot command line.
2608 * console="" or console=null have been suggested as a way to in console_setup()
2609 * disable console output. Use ttynull that has been created in console_setup()
2620 /* For a DEVNAME:0.0 style console the character device is unknown early */ in console_setup()
2651 idx = -1; in console_setup()
2660 __setup("console=", console_setup);
2663 * add_preferred_console - add a device to the list of preferred consoles.
2666 * @options: options for this console
2668 * The last preferred console added will be used for kernel messages
2670 * above to handle user-supplied console arguments; however it can also
2671 * be used by arch-specific code either to override the user or more
2672 * commonly to provide a default console (ie from PROM variables) when
2681 * match_devname_and_update_preferred_console - Update a preferred console
2684 * @name: Name of the corresponding console driver, e.g. "ttyS"
2685 * @idx: Console index, e.g. port number.
2688 * preferred via the console=DEVNAME:0.0 command line option.
2689 * It fills the missing console driver name and console index
2695 * could predict which console driver name and index this device
2708 return -EINVAL; in match_devname_and_update_preferred_console()
2710 for (i = 0; i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]); in match_devname_and_update_preferred_console()
2712 if (!strcmp(devname, c->devname)) { in match_devname_and_update_preferred_console()
2713 pr_info("associate the preferred console \"%s\" with \"%s%d\"\n", in match_devname_and_update_preferred_console()
2715 strscpy(c->name, name); in match_devname_and_update_preferred_console()
2716 c->index = idx; in match_devname_and_update_preferred_console()
2721 return -ENOENT; in match_devname_and_update_preferred_console()
2736 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2749 MODULE_PARM_DESC(console_no_auto_verbose, "Disable console loglevel raise to highest on oops/panic/…
2752 * console_suspend_all - suspend the console subsystem
2758 struct console *con; in console_suspend_all()
2762 pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); in console_suspend_all()
2767 console_srcu_write_flags(con, con->flags | CON_SUSPENDED); in console_suspend_all()
2782 struct console *con; in console_resume_all()
2789 console_srcu_write_flags(con, con->flags & ~CON_SUSPENDED); in console_resume_all()
2809 * console_cpu_notify - print deferred console messages after CPU hotplug
2813 * will be printed on the console only if there are CON_ANYTIME consoles.
2834 * console_lock - block the console subsystem from printing
2856 * console_trylock - try to block the console subsystem from printing
2891 * Prepend the message in @pmsg->pbufs->outbuf. This is achieved by shifting
2897 * If there is not enough space in @pmsg->pbufs->outbuf, the existing
2900 * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
2905 struct printk_buffers *pbufs = pmsg->pbufs; in console_prepend_message()
2906 const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); in console_prepend_message()
2907 const size_t outbuf_sz = sizeof(pbufs->outbuf); in console_prepend_message()
2908 char *scratchbuf = &pbufs->scratchbuf[0]; in console_prepend_message()
2909 char *outbuf = &pbufs->outbuf[0]; in console_prepend_message()
2926 if (pmsg->outbuf_len + len >= outbuf_sz) { in console_prepend_message()
2928 pmsg->outbuf_len = outbuf_sz - (len + 1); in console_prepend_message()
2929 outbuf[pmsg->outbuf_len] = 0; in console_prepend_message()
2932 memmove(outbuf + len, outbuf, pmsg->outbuf_len + 1); in console_prepend_message()
2934 pmsg->outbuf_len += len; in console_prepend_message()
2938 * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message".
2939 * @pmsg->outbuf_len is updated appropriately.
2951 * Prepend the message in @pmsg->pbufs->outbuf with a "replay message".
2952 * @pmsg->outbuf_len is updated appropriately.
2965 * @pmsg will contain the formatted result. @pmsg->pbufs must point to a
2972 * console output.
2983 struct printk_buffers *pbufs = pmsg->pbufs; in printk_get_next_message()
2984 const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); in printk_get_next_message()
2985 const size_t outbuf_sz = sizeof(pbufs->outbuf); in printk_get_next_message()
2986 char *scratchbuf = &pbufs->scratchbuf[0]; in printk_get_next_message()
2987 char *outbuf = &pbufs->outbuf[0]; in printk_get_next_message()
2997 * Formatting normal messages is done in-place, so read the ringbuffer in printk_get_next_message()
3008 pmsg->seq = r.info->seq; in printk_get_next_message()
3009 pmsg->dropped = r.info->seq - seq; in printk_get_next_message()
3010 force_con = r.info->flags & LOG_FORCE_CON; in printk_get_next_message()
3014 * has level above the console loglevel. in printk_get_next_message()
3016 if (!force_con && may_suppress && suppress_message_printing(r.info->level)) in printk_get_next_message()
3021 len += msg_print_ext_body(outbuf + len, outbuf_sz - len, in printk_get_next_message()
3022 &r.text_buf[0], r.info->text_len, &r.info->dev_info); in printk_get_next_message()
3027 pmsg->outbuf_len = len; in printk_get_next_message()
3032 * Legacy console printing from printk() caller context does not respect
3038 * console write() callback when legacy printing to avoid false positive
3060 * Used as the printk buffers for non-panic, serialized console printing.
3067 * Print one record for the given console. The record printed is whatever
3068 * record is the next available record for the given console.
3076 * Returns false if the given console has no next record to print, otherwise
3081 static bool console_emit_next_record(struct console *con, bool *handover, int cookie) in console_emit_next_record()
3092 if (!printk_get_next_message(&pmsg, con->seq, is_extended, true)) in console_emit_next_record()
3095 con->dropped += pmsg.dropped; in console_emit_next_record()
3099 con->seq = pmsg.seq + 1; in console_emit_next_record()
3103 if (con->dropped && !is_extended) { in console_emit_next_record()
3104 console_prepend_dropped(&pmsg, con->dropped); in console_emit_next_record()
3105 con->dropped = 0; in console_emit_next_record()
3118 con->write(con, outbuf, pmsg.outbuf_len); in console_emit_next_record()
3119 con->seq = pmsg.seq + 1; in console_emit_next_record()
3138 con->write(con, outbuf, pmsg.outbuf_len); in console_emit_next_record()
3143 con->seq = pmsg.seq + 1; in console_emit_next_record()
3154 static bool console_emit_next_record(struct console *con, bool *handover, int cookie) in console_emit_next_record()
3178 * Returns true when there was at least one usable console and all messages
3191 struct console *con; in console_flush_all()
3227 printk_seq = con->seq; in console_flush_all()
3270 * Console drivers are called with interrupts disabled, so in __console_flush_and_unlock()
3273 * console registration path, and should invoke cond_resched() in __console_flush_and_unlock()
3275 * scheduling stall on a slow console leading to RCU stall and in __console_flush_and_unlock()
3300 * console_flush_all() but before unlocking the console. in __console_flush_and_unlock()
3301 * Re-check if there is a new record to flush. If the trylock in __console_flush_and_unlock()
3308 * console_unlock - unblock the legacy console subsystem from printing
3311 * the legacy console subsystem.
3313 * While the console_lock was held, console output may have been buffered
3332 * console_conditional_schedule - yield the CPU if required
3334 * If the console code is currently allowed to sleep, and
3350 struct console *c; in console_unblank()
3356 * console lock, which in particular can be dangerous if in console_unblank()
3366 if ((flags & CON_ENABLED) && c->unblank) { in console_unblank()
3376 * Stop console printing because the unblank() callback may in console_unblank()
3377 * assume the console is not within its write() callback. in console_unblank()
3380 * In that case, attempt a trylock as best-effort. in console_unblank()
3383 /* Semaphores are not NMI-safe. */ in console_unblank()
3388 * Attempting to trylock the console lock can deadlock in console_unblank()
3408 if ((flags & CON_ENABLED) && c->unblank) in console_unblank()
3409 c->unblank(); in console_unblank()
3428 struct console *c; in __console_rewind_all()
3447 c->seq = seq; in __console_rewind_all()
3454 * console_flush_on_panic - flush console content on panic
3466 * Ignore the console lock and flush out the messages. Attempting a in console_flush_on_panic()
3469 * - if it is contended, it must be ignored anyway in console_flush_on_panic()
3470 * - console_lock() and console_trylock() block and fail in console_flush_on_panic()
3471 * respectively in panic for non-panic CPUs in console_flush_on_panic()
3472 * - semaphores are not NMI-safe in console_flush_on_panic()
3476 * If another context is holding the console lock, in console_flush_on_panic()
3495 * Return the console tty driver structure and its associated index
3499 struct console *c; in console_device()
3505 * other console operations. For example, fg_console is in console_device()
3512 if (!c->device) in console_device()
3514 driver = c->device(c, index); in console_device()
3525 * Prevent further output on the passed console device so that (for example)
3526 * serial drivers can suspend console output before suspending a port, and can
3527 * re-enable output afterwards.
3529 void console_suspend(struct console *console) in console_suspend() argument
3531 __pr_flush(console, 1000, true); in console_suspend()
3533 console_srcu_write_flags(console, console->flags & ~CON_ENABLED); in console_suspend()
3538 * be able to see that this console is disabled so that (for example) in console_suspend()
3546 void console_resume(struct console *console) in console_resume() argument
3552 console_srcu_write_flags(console, console->flags | CON_ENABLED); in console_resume()
3553 is_nbcon = console->flags & CON_NBCON; in console_resume()
3565 nbcon_kthread_wake(console); in console_resume()
3569 __pr_flush(console, 1000, true); in console_resume()
3574 static int unregister_console_locked(struct console *console);
3584 struct console *con; in legacy_kthread_should_wakeup()
3616 printk_seq = con->seq; in legacy_kthread_should_wakeup()
3659 * It is important that console printing threads are scheduled in legacy_kthread_create()
3662 sched_set_normal(printk_legacy_kthread, -20); in legacy_kthread_create()
3668 * printk_kthreads_shutdown - shutdown all threaded printers
3676 struct console *con; in printk_kthreads_shutdown()
3683 if (con->flags & CON_NBCON) in printk_kthreads_shutdown()
3709 struct console *con; in printk_kthreads_check_locked()
3726 if (con->flags & CON_NBCON) in printk_kthreads_check_locked()
3738 * Printer threads cannot be started as long as any boot console is in printk_kthreads_check_locked()
3740 * registers between boot console code and regular console code. in printk_kthreads_check_locked()
3742 * an nbcon console is registered. in printk_kthreads_check_locked()
3754 if (!(con->flags & CON_NBCON)) in printk_kthreads_check_locked()
3783 pr_info("debug: skip boot console de-registration.\n"); in keep_bootcon_setup()
3790 static int console_call_setup(struct console *newcon, char *options) in console_call_setup()
3794 if (!newcon->setup) in console_call_setup()
3797 /* Synchronize with possible boot console. */ in console_call_setup()
3799 err = newcon->setup(newcon, options); in console_call_setup()
3807 * the newly registered console with any of the ones selected
3814 static int try_enable_preferred_console(struct console *newcon, in try_enable_preferred_console()
3821 i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]); in try_enable_preferred_console()
3823 /* Console not yet initialized? */ in try_enable_preferred_console()
3824 if (!c->name[0]) in try_enable_preferred_console()
3826 if (c->user_specified != user_specified) in try_enable_preferred_console()
3828 if (!newcon->match || in try_enable_preferred_console()
3829 newcon->match(newcon, c->name, c->index, c->options) != 0) { in try_enable_preferred_console()
3831 BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); in try_enable_preferred_console()
3832 if (strcmp(c->name, newcon->name) != 0) in try_enable_preferred_console()
3834 if (newcon->index >= 0 && in try_enable_preferred_console()
3835 newcon->index != c->index) in try_enable_preferred_console()
3837 if (newcon->index < 0) in try_enable_preferred_console()
3838 newcon->index = c->index; in try_enable_preferred_console()
3843 err = console_call_setup(newcon, c->options); in try_enable_preferred_console()
3847 newcon->flags |= CON_ENABLED; in try_enable_preferred_console()
3849 newcon->flags |= CON_CONSDEV; in try_enable_preferred_console()
3855 * without matching. Accept the pre-enabled consoles only when match() in try_enable_preferred_console()
3858 if (newcon->flags & CON_ENABLED && c->user_specified == user_specified) in try_enable_preferred_console()
3861 return -ENOENT; in try_enable_preferred_console()
3864 /* Try to enable the console unconditionally */
3865 static void try_enable_default_console(struct console *newcon) in try_enable_default_console()
3867 if (newcon->index < 0) in try_enable_default_console()
3868 newcon->index = 0; in try_enable_default_console()
3873 newcon->flags |= CON_ENABLED; in try_enable_default_console()
3875 if (newcon->device) in try_enable_default_console()
3876 newcon->flags |= CON_CONSDEV; in try_enable_default_console()
3879 /* Return the starting sequence number for a newly registered console. */
3880 static u64 get_init_console_seq(struct console *newcon, bool bootcon_registered) in get_init_console_seq()
3882 struct console *con; in get_init_console_seq()
3886 if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) { in get_init_console_seq()
3898 * device as @newcon. Since it is not known which boot console in get_init_console_seq()
3900 * start with the message of the enabled boot console that is in get_init_console_seq()
3905 * Hold the console_lock to stop console printing and in get_init_console_seq()
3906 * guarantee safe access to console->seq. in get_init_console_seq()
3911 * Flush all consoles and set the console to start at in get_init_console_seq()
3931 if (!(con->flags & CON_BOOT) || in get_init_console_seq()
3932 !(con->flags & CON_ENABLED)) { in get_init_console_seq()
3936 if (con->flags & CON_NBCON) in get_init_console_seq()
3939 seq = con->seq; in get_init_console_seq()
3954 hlist_entry(console_list.first, struct console, node)
3956 static int unregister_console_locked(struct console *console);
3959 * The console driver calls this routine during kernel initialization
3960 * to register the console printing procedure with printk() and to
3962 * console driver was initialized.
3965 * early_printk) - sometimes before setup_arch() completes - be careful
3966 * of what kernel features are used - they may not be initialised yet.
3968 * There are two types of consoles - bootconsoles (early_printk) and
3971 * - Any number of bootconsoles can be registered at any time.
3972 * - As soon as a "real" console is registered, all bootconsoles
3974 * - Once a "real" console is registered, any attempt to register a
3977 void register_console(struct console *newcon) in register_console()
3979 bool use_device_lock = (newcon->flags & CON_NBCON) && newcon->write_atomic; in register_console()
3982 struct console *con; in register_console()
3990 if (WARN(con == newcon, "console '%s%d' already registered\n", in register_console()
3991 con->name, con->index)) { in register_console()
3995 if (con->flags & CON_BOOT) in register_console()
4002 if ((newcon->flags & CON_BOOT) && realcon_registered) { in register_console()
4004 newcon->name, newcon->index); in register_console()
4008 if (newcon->flags & CON_NBCON) { in register_console()
4010 * Ensure the nbcon console buffers can be allocated in register_console()
4018 * See if we want to enable this console driver by default. in register_console()
4020 * Nope when a console is preferred by the command line, device in register_console()
4023 * The first real console with tty binding (driver) wins. More in register_console()
4026 * Note that a console with tty binding will have CON_CONSDEV in register_console()
4030 if (hlist_empty(&console_list) || !console_first()->device || in register_console()
4031 console_first()->flags & CON_BOOT) { in register_console()
4036 /* See if this console matches one we selected on the command line */ in register_console()
4040 if (err == -ENOENT) in register_console()
4043 /* printk() messages are not printed to the Braille console. */ in register_console()
4044 if (err || newcon->flags & CON_BRL) { in register_console()
4045 if (newcon->flags & CON_NBCON) in register_console()
4051 * If we have a bootconsole, and are switching to a real console, in register_console()
4052 * don't print everything out again, since when the boot console, and in register_console()
4053 * the real console are the same physical device, it's annoying to in register_console()
4057 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) { in register_console()
4058 newcon->flags &= ~CON_PRINTBUFFER; in register_console()
4061 newcon->dropped = 0; in register_console()
4064 if (newcon->flags & CON_NBCON) { in register_console()
4069 newcon->seq = init_seq; in register_console()
4072 if (newcon->flags & CON_BOOT) in register_console()
4077 * console, it will not be aware of the nbcon synchronization. This in register_console()
4079 * simultaneously if this new console is used for atomic printing in register_console()
4083 * in use while this new console transitions to being registered. in register_console()
4086 newcon->device_lock(newcon, &flags); in register_console()
4089 * Put this console in the list - keep the in register_console()
4094 newcon->flags |= CON_CONSDEV; in register_console()
4095 hlist_add_head_rcu(&newcon->node, &console_list); in register_console()
4097 } else if (newcon->flags & CON_CONSDEV) { in register_console()
4099 console_srcu_write_flags(console_first(), console_first()->flags & ~CON_CONSDEV); in register_console()
4100 hlist_add_head_rcu(&newcon->node, &console_list); in register_console()
4103 hlist_add_behind_rcu(&newcon->node, console_list.first); in register_console()
4108 * on all contexts being able to see the new console before in register_console()
4112 /* This new console is now registered. */ in register_console()
4114 newcon->device_unlock(newcon, flags); in register_console()
4119 * By unregistering the bootconsoles after we enable the real console in register_console()
4120 * we get the "console xxx enabled" message on all the consoles - in register_console()
4121 * boot consoles, real consoles, etc - this is to ensure that end in register_console()
4123 * went to the bootconsole (that they do not see on the real console) in register_console()
4127 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && in register_console()
4132 if (con->flags & CON_BOOT) in register_console()
4137 /* Changed console list, may require printer threads to start/stop. */ in register_console()
4145 static int unregister_console_locked(struct console *console) in unregister_console_locked() argument
4147 bool use_device_lock = (console->flags & CON_NBCON) && console->write_atomic; in unregister_console_locked()
4152 struct console *c; in unregister_console_locked()
4157 con_printk(KERN_INFO, console, "disabled\n"); in unregister_console_locked()
4159 res = _braille_unregister_console(console); in unregister_console_locked()
4165 if (!console_is_registered_locked(console)) in unregister_console_locked()
4166 res = -ENODEV; in unregister_console_locked()
4167 else if (console_is_usable(console, console->flags, true)) in unregister_console_locked()
4168 __pr_flush(console, 1000, true); in unregister_console_locked()
4171 console_srcu_write_flags(console, console->flags & ~CON_ENABLED); in unregister_console_locked()
4178 * in use while this console transitions to being unregistered. in unregister_console_locked()
4181 console->device_lock(console, &flags); in unregister_console_locked()
4183 hlist_del_init_rcu(&console->node); in unregister_console_locked()
4186 console->device_unlock(console, flags); in unregister_console_locked()
4190 * If this isn't the last console and it has CON_CONSDEV set, we in unregister_console_locked()
4191 * need to set it on the next preferred console. in unregister_console_locked()
4195 * console has any device attached. Oh well.... in unregister_console_locked()
4197 if (!hlist_empty(&console_list) && console->flags & CON_CONSDEV) in unregister_console_locked()
4198 console_srcu_write_flags(console_first(), console_first()->flags | CON_CONSDEV); in unregister_console_locked()
4202 * must not be able to see this console in the list so that any in unregister_console_locked()
4207 if (console->flags & CON_NBCON) in unregister_console_locked()
4208 nbcon_free(console); in unregister_console_locked()
4212 if (console->exit) in unregister_console_locked()
4213 res = console->exit(console); in unregister_console_locked()
4216 * With this console gone, the global flags tracking registered in unregister_console_locked()
4217 * console types may have changed. Update them. in unregister_console_locked()
4220 if (c->flags & CON_BOOT) in unregister_console_locked()
4223 if (c->flags & CON_NBCON) in unregister_console_locked()
4235 /* Changed console list, may require printer threads to start/stop. */ in unregister_console_locked()
4241 int unregister_console(struct console *console) in unregister_console() argument
4246 res = unregister_console_locked(console); in unregister_console()
4253 * console_force_preferred_locked - force a registered console preferred
4254 * @con: The registered console to force preferred.
4258 void console_force_preferred_locked(struct console *con) in console_force_preferred_locked()
4260 struct console *cur_pref_con; in console_force_preferred_locked()
4272 * Delete, but do not re-initialize the entry. This allows the console in console_force_preferred_locked()
4274 * checks), even though it was briefly removed from the console list. in console_force_preferred_locked()
4276 hlist_del_rcu(&con->node); in console_force_preferred_locked()
4279 * Ensure that all SRCU list walks have completed so that the console in console_force_preferred_locked()
4280 * can be added to the beginning of the console list and its forward in console_force_preferred_locked()
4281 * list pointer can be re-initialized. in console_force_preferred_locked()
4285 con->flags |= CON_CONSDEV; in console_force_preferred_locked()
4286 WARN_ON(!con->device); in console_force_preferred_locked()
4289 console_srcu_write_flags(cur_pref_con, cur_pref_con->flags & ~CON_CONSDEV); in console_force_preferred_locked()
4290 hlist_add_head_rcu(&con->node, &console_list); in console_force_preferred_locked()
4295 * Initialize the console device. This is called *early*, so
4315 * set up the console device so that later boot sequences can in console_init()
4319 trace_initcall_level("console"); in console_init()
4335 * module, the real console hasn't registered yet at this point, there will
4336 * be a brief interval in which no messages are logged to the console, which
4341 * get unregistered when the real preferred console is registered.
4346 struct console *con; in printk_late_init()
4351 if (!(con->flags & CON_BOOT)) in printk_late_init()
4356 init_section_contains(con->write, 0) || in printk_late_init()
4357 init_section_contains(con->read, 0) || in printk_late_init()
4358 init_section_contains(con->device, 0) || in printk_late_init()
4359 init_section_contains(con->unblank, 0) || in printk_late_init()
4360 init_section_contains(con->data, 0)) { in printk_late_init()
4366 con->name, con->index); in printk_late_init()
4384 /* If @con is specified, only wait for that console. Otherwise wait for all. */
4385 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) in __pr_flush()
4390 struct console *c; in __pr_flush()
4423 * console->seq. Releasing console_lock flushes more in __pr_flush()
4429 * console could register at any time. Always hold the in __pr_flush()
4455 printk_seq = c->seq; in __pr_flush()
4459 diff += seq - printk_seq; in __pr_flush()
4475 slept_jiffies = jiffies - begin_jiffies; in __pr_flush()
4477 remaining_jiffies -= min(slept_jiffies, remaining_jiffies); in __pr_flush()
4486 * pr_flush() - Wait for printing threads to catch up.
4491 * A value of 0 for @timeout_ms means no waiting will occur. A value of -1
4497 * Context: Process context. May sleep while acquiring console lock.
4506 * Delayed printk version, for scheduler-internal messages:
4560 * wake_up_klogd - Wake kernel logging daemon
4563 * and the console printing of those records has already occurred or is
4575 * defer_console_output - Wake kernel logging daemon and trigger
4576 * console printing in a deferred context
4579 * this context is responsible for console printing those records, but
4580 * the current context is not allowed to perform the console printing.
4581 * Trigger an irq_work context to perform the console printing. This
4621 * every 5s to make a denial-of-service attack impossible.
4632 * printk_timed_ratelimit - caller-controlled printk ratelimiting
4643 unsigned long elapsed = jiffies - *caller_jiffies; in printk_timed_ratelimit()
4657 * kmsg_dump_register - register a kernel log dumper.
4662 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
4667 int err = -EBUSY; in kmsg_dump_register()
4670 if (!dumper->dump) in kmsg_dump_register()
4671 return -EINVAL; in kmsg_dump_register()
4675 if (!dumper->registered) { in kmsg_dump_register()
4676 dumper->registered = 1; in kmsg_dump_register()
4677 list_add_tail_rcu(&dumper->list, &dump_list); in kmsg_dump_register()
4687 * kmsg_dump_unregister - unregister a kmsg dumper.
4691 * %-EINVAL otherwise.
4696 int err = -EINVAL; in kmsg_dump_unregister()
4699 if (dumper->registered) { in kmsg_dump_unregister()
4700 dumper->registered = 0; in kmsg_dump_unregister()
4701 list_del_rcu(&dumper->list); in kmsg_dump_unregister()
4732 * kmsg_dump_desc - dump kernel log to kernel message dumpers.
4750 enum kmsg_dump_reason max_reason = dumper->max_reason; in kmsg_dump_desc()
4764 dumper->dump(dumper, &detail); in kmsg_dump_desc()
4770 * kmsg_dump_get_line - retrieve one kmsg log line
4796 if (iter->cur_seq < min_seq) in kmsg_dump_get_line()
4797 iter->cur_seq = min_seq; in kmsg_dump_get_line()
4803 if (!prb_read_valid(prb, iter->cur_seq, &r)) in kmsg_dump_get_line()
4807 if (!prb_read_valid_info(prb, iter->cur_seq, in kmsg_dump_get_line()
4816 iter->cur_seq = r.info->seq + 1; in kmsg_dump_get_line()
4826 * kmsg_dump_get_buffer - copy kmsg log lines
4859 if (iter->cur_seq < min_seq) in kmsg_dump_get_buffer()
4860 iter->cur_seq = min_seq; in kmsg_dump_get_buffer()
4862 if (prb_read_valid_info(prb, iter->cur_seq, &info, NULL)) { in kmsg_dump_get_buffer()
4863 if (info.seq != iter->cur_seq) { in kmsg_dump_get_buffer()
4865 iter->cur_seq = info.seq; in kmsg_dump_get_buffer()
4870 if (iter->cur_seq >= iter->next_seq) in kmsg_dump_get_buffer()
4875 * into the user-provided buffer for this dump. Pass in size-1 in kmsg_dump_get_buffer()
4877 * not write more than size-1 bytes of text into @buf. in kmsg_dump_get_buffer()
4879 seq = find_first_fitting_seq(iter->cur_seq, iter->next_seq, in kmsg_dump_get_buffer()
4880 size - 1, syslog, time); in kmsg_dump_get_buffer()
4891 if (r.info->seq >= iter->next_seq) in kmsg_dump_get_buffer()
4897 prb_rec_init_rd(&r, &info, buf + len, size - len); in kmsg_dump_get_buffer()
4900 iter->next_seq = next_seq; in kmsg_dump_get_buffer()
4910 * kmsg_dump_rewind - reset the iterator
4919 iter->cur_seq = latched_seq_read_nolock(&clear_seq); in kmsg_dump_rewind()
4920 iter->next_seq = prb_next_seq(prb); in kmsg_dump_rewind()
4925 * console_try_replay_all - try to replay kernel log on consoles
4927 * Try to obtain lock on console subsystem and replay all
4953 static atomic_t printk_cpu_sync_owner = ATOMIC_INIT(-1);
4962 * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant
4971 } while (atomic_read(&printk_cpu_sync_owner) != -1); in __printk_cpu_sync_wait()
4976 * __printk_cpu_sync_try_get() - Try to acquire the printk cpu-reentrant
5012 old = atomic_cmpxchg_acquire(&printk_cpu_sync_owner, -1, in __printk_cpu_sync_try_get()
5014 if (old == -1) { in __printk_cpu_sync_try_get()
5032 * __printk_cpu_sync_put() - Release the printk cpu-reentrant spinning lock.
5069 -1); /* LMM(__printk_cpu_sync_put:B) */ in __printk_cpu_sync_put()