Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'printk-for-5.18' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux

Pull printk updates from Petr Mladek:

- Make %pK behave the same as %p for kptr_restrict == 0 also with
no_hash_pointers parameter

- Ignore the default console in the device tree also when console=null
or console="" is used on the command line

- Document console=null and console="" behavior

- Prevent a deadlock and a livelock caused by console_lock in panic()

- Make console_lock available for panicking CPU

- Fast query for the next to-be-used sequence number

- Use the expected return values in printk.devkmsg __setup handler

- Use the correct atomic operations in wake_up_klogd() irq_work handler

- Avoid possible unaligned access when handling %4cc printing format

* tag 'printk-for-5.18' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux:
printk: fix return value of printk.devkmsg __setup handler
vsprintf: Fix %pK with kptr_restrict == 0
printk: make suppress_panic_printk static
printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true
Docs: printk: add 'console=null|""' to admin/kernel-parameters
printk: use atomic updates for klogd work
printk: Drop console_sem during panic
printk: Avoid livelock with heavy printk during panic
printk: disable optimistic spin during panic
printk: Add panic_in_progress helper
vsprintf: Move space out of string literals in fourcc_string()
vsprintf: Fix potential unaligned access
printk: ringbuffer: Improve prb_next_seq() performance

+161 -36
+7 -2
Documentation/admin-guide/kernel-parameters.txt
··· 724 724 hvc<n> Use the hypervisor console device <n>. This is for 725 725 both Xen and PowerPC hypervisors. 726 726 727 + { null | "" } 728 + Use to disable console output, i.e., to have kernel 729 + console messages discarded. 730 + This must be the only console= parameter used on the 731 + kernel command line. 732 + 727 733 If the device connected to the port is not a TTY but a braille 728 734 device, prepend "brl," before the device type, for instance 729 735 console=brl,ttyS0 ··· 3522 3516 difficult since unequal pointers can no longer be 3523 3517 compared. However, if this command-line option is 3524 3518 specified, then all normal pointers will have their true 3525 - value printed. Pointers printed via %pK may still be 3526 - hashed. This option should only be specified when 3519 + value printed. This option should only be specified when 3527 3520 debugging the kernel. Please do not use on production 3528 3521 kernels. 3529 3522
+76 -9
kernel/printk/printk.c
··· 93 93 */ 94 94 int __read_mostly suppress_printk; 95 95 96 + /* 97 + * During panic, heavy printk by other CPUs can delay the 98 + * panic and risk deadlock on console resources. 99 + */ 100 + static int __read_mostly suppress_panic_printk; 101 + 96 102 #ifdef CONFIG_LOCKDEP 97 103 static struct lockdep_map console_lock_dep_map = { 98 104 .name = "console_lock" ··· 152 146 153 147 static int __init control_devkmsg(char *str) 154 148 { 155 - if (__control_devkmsg(str) < 0) 149 + if (__control_devkmsg(str) < 0) { 150 + pr_warn("printk.devkmsg: bad option string '%s'\n", str); 156 151 return 1; 152 + } 157 153 158 154 /* 159 155 * Set sysctl string accordingly: ··· 174 166 */ 175 167 devkmsg_log |= DEVKMSG_LOG_MASK_LOCK; 176 168 177 - return 0; 169 + return 1; 178 170 } 179 171 __setup("printk.devkmsg=", control_devkmsg); 180 172 ··· 264 256 printk_safe_exit_irqrestore(flags); 265 257 } 266 258 #define up_console_sem() __up_console_sem(_RET_IP_) 259 + 260 + static bool panic_in_progress(void) 261 + { 262 + return unlikely(atomic_read(&panic_cpu) != PANIC_CPU_INVALID); 263 + } 267 264 268 265 /* 269 266 * This is used for debugging the mess that is the VT code by ··· 1856 1843 if (console_trylock()) 1857 1844 return 1; 1858 1845 1846 + /* 1847 + * It's unsafe to spin once a panic has begun. If we are the 1848 + * panic CPU, we may have already halted the owner of the 1849 + * console_sem. If we are not the panic CPU, then we should 1850 + * avoid taking console_sem, so the panic CPU has a better 1851 + * chance of cleanly acquiring it later. 1852 + */ 1853 + if (panic_in_progress()) 1854 + return 0; 1855 + 1859 1856 printk_safe_enter_irqsave(flags); 1860 1857 1861 1858 raw_spin_lock(&console_owner_lock); ··· 2241 2218 if (unlikely(suppress_printk)) 2242 2219 return 0; 2243 2220 2221 + if (unlikely(suppress_panic_printk) && 2222 + atomic_read(&panic_cpu) != raw_smp_processor_id()) 2223 + return 0; 2224 + 2244 2225 if (level == LOGLEVEL_SCHED) { 2245 2226 level = LOGLEVEL_DEFAULT; 2246 2227 in_sched = true; ··· 2351 2324 } 2352 2325 #endif 2353 2326 2327 + static void set_user_specified(struct console_cmdline *c, bool user_specified) 2328 + { 2329 + if (!user_specified) 2330 + return; 2331 + 2332 + /* 2333 + * @c console was defined by the user on the command line. 2334 + * Do not clear when added twice also by SPCR or the device tree. 2335 + */ 2336 + c->user_specified = true; 2337 + /* At least one console defined by the user on the command line. */ 2338 + console_set_on_cmdline = 1; 2339 + } 2340 + 2354 2341 static int __add_preferred_console(char *name, int idx, char *options, 2355 2342 char *brl_options, bool user_specified) 2356 2343 { ··· 2381 2340 if (strcmp(c->name, name) == 0 && c->index == idx) { 2382 2341 if (!brl_options) 2383 2342 preferred_console = i; 2384 - if (user_specified) 2385 - c->user_specified = true; 2343 + set_user_specified(c, user_specified); 2386 2344 return 0; 2387 2345 } 2388 2346 } ··· 2391 2351 preferred_console = i; 2392 2352 strlcpy(c->name, name, sizeof(c->name)); 2393 2353 c->options = options; 2394 - c->user_specified = user_specified; 2354 + set_user_specified(c, user_specified); 2395 2355 braille_set_options(c, brl_options); 2396 2356 2397 2357 c->index = idx; ··· 2457 2417 *s = 0; 2458 2418 2459 2419 __add_preferred_console(buf, idx, options, brl_options, true); 2460 - console_set_on_cmdline = 1; 2461 2420 return 1; 2462 2421 } 2463 2422 __setup("console=", console_setup); ··· 2613 2574 } 2614 2575 2615 2576 /* 2577 + * Return true when this CPU should unlock console_sem without pushing all 2578 + * messages to the console. This reduces the chance that the console is 2579 + * locked when the panic CPU tries to use it. 2580 + */ 2581 + static bool abandon_console_lock_in_panic(void) 2582 + { 2583 + if (!panic_in_progress()) 2584 + return false; 2585 + 2586 + /* 2587 + * We can use raw_smp_processor_id() here because it is impossible for 2588 + * the task to be migrated to the panic_cpu, or away from it. If 2589 + * panic_cpu has already been set, and we're not currently executing on 2590 + * that CPU, then we never will be. 2591 + */ 2592 + return atomic_read(&panic_cpu) != raw_smp_processor_id(); 2593 + } 2594 + 2595 + /* 2616 2596 * Can we actually use the console at this time on this cpu? 2617 2597 * 2618 2598 * Console drivers may assume that per-cpu resources have been allocated. So ··· 2661 2603 { 2662 2604 static char ext_text[CONSOLE_EXT_LOG_MAX]; 2663 2605 static char text[CONSOLE_LOG_MAX]; 2606 + static int panic_console_dropped; 2664 2607 unsigned long flags; 2665 2608 bool do_cond_resched, retry; 2666 2609 struct printk_info info; ··· 2716 2657 if (console_seq != r.info->seq) { 2717 2658 console_dropped += r.info->seq - console_seq; 2718 2659 console_seq = r.info->seq; 2660 + if (panic_in_progress() && panic_console_dropped++ > 10) { 2661 + suppress_panic_printk = 1; 2662 + pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); 2663 + } 2719 2664 } 2720 2665 2721 2666 if (suppress_message_printing(r.info->level)) { ··· 2779 2716 if (handover) 2780 2717 return; 2781 2718 2719 + /* Allow panic_cpu to take over the consoles safely */ 2720 + if (abandon_console_lock_in_panic()) 2721 + break; 2722 + 2782 2723 if (do_cond_resched) 2783 2724 cond_resched(); 2784 2725 } ··· 2800 2733 * flush, no worries. 2801 2734 */ 2802 2735 retry = prb_read_valid(prb, next_seq, NULL); 2803 - if (retry && console_trylock()) 2736 + if (retry && !abandon_console_lock_in_panic() && console_trylock()) 2804 2737 goto again; 2805 2738 } 2806 2739 EXPORT_SYMBOL(console_unlock); ··· 3295 3228 3296 3229 static void wake_up_klogd_work_func(struct irq_work *irq_work) 3297 3230 { 3298 - int pending = __this_cpu_xchg(printk_pending, 0); 3231 + int pending = this_cpu_xchg(printk_pending, 0); 3299 3232 3300 3233 if (pending & PRINTK_PENDING_OUTPUT) { 3301 3234 /* If trylock fails, someone else is doing the printing */ ··· 3329 3262 return; 3330 3263 3331 3264 preempt_disable(); 3332 - __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT); 3265 + this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT); 3333 3266 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); 3334 3267 preempt_enable(); 3335 3268 }
+47 -5
kernel/printk/printk_ringbuffer.c
··· 474 474 * state has been re-checked. A memcpy() for all of @desc 475 475 * cannot be used because of the atomic_t @state_var field. 476 476 */ 477 - memcpy(&desc_out->text_blk_lpos, &desc->text_blk_lpos, 478 - sizeof(desc_out->text_blk_lpos)); /* LMM(desc_read:C) */ 477 + if (desc_out) { 478 + memcpy(&desc_out->text_blk_lpos, &desc->text_blk_lpos, 479 + sizeof(desc_out->text_blk_lpos)); /* LMM(desc_read:C) */ 480 + } 479 481 if (seq_out) 480 482 *seq_out = info->seq; /* also part of desc_read:C */ 481 483 if (caller_id_out) ··· 530 528 state_val = atomic_long_read(state_var); /* LMM(desc_read:E) */ 531 529 d_state = get_desc_state(id, state_val); 532 530 out: 533 - atomic_long_set(&desc_out->state_var, state_val); 531 + if (desc_out) 532 + atomic_long_set(&desc_out->state_var, state_val); 534 533 return d_state; 535 534 } 536 535 ··· 1452 1449 1453 1450 atomic_long_cmpxchg_relaxed(&d->state_var, prev_state_val, 1454 1451 DESC_SV(id, desc_finalized)); /* LMM(desc_make_final:A) */ 1452 + 1453 + /* Best effort to remember the last finalized @id. */ 1454 + atomic_long_set(&desc_ring->last_finalized_id, id); 1455 1455 } 1456 1456 1457 1457 /** ··· 1663 1657 */ 1664 1658 void prb_final_commit(struct prb_reserved_entry *e) 1665 1659 { 1660 + struct prb_desc_ring *desc_ring = &e->rb->desc_ring; 1661 + 1666 1662 _prb_commit(e, desc_finalized); 1663 + 1664 + /* Best effort to remember the last finalized @id. */ 1665 + atomic_long_set(&desc_ring->last_finalized_id, e->id); 1667 1666 } 1668 1667 1669 1668 /* ··· 2016 2005 */ 2017 2006 u64 prb_next_seq(struct printk_ringbuffer *rb) 2018 2007 { 2019 - u64 seq = 0; 2008 + struct prb_desc_ring *desc_ring = &rb->desc_ring; 2009 + enum desc_state d_state; 2010 + unsigned long id; 2011 + u64 seq; 2020 2012 2021 - /* Search forward from the oldest descriptor. */ 2013 + /* Check if the cached @id still points to a valid @seq. */ 2014 + id = atomic_long_read(&desc_ring->last_finalized_id); 2015 + d_state = desc_read(desc_ring, id, NULL, &seq, NULL); 2016 + 2017 + if (d_state == desc_finalized || d_state == desc_reusable) { 2018 + /* 2019 + * Begin searching after the last finalized record. 2020 + * 2021 + * On 0, the search must begin at 0 because of hack#2 2022 + * of the bootstrapping phase it is not known if a 2023 + * record at index 0 exists. 2024 + */ 2025 + if (seq != 0) 2026 + seq++; 2027 + } else { 2028 + /* 2029 + * The information about the last finalized sequence number 2030 + * has gone. It should happen only when there is a flood of 2031 + * new messages and the ringbuffer is rapidly recycled. 2032 + * Give up and start from the beginning. 2033 + */ 2034 + seq = 0; 2035 + } 2036 + 2037 + /* 2038 + * The information about the last finalized @seq might be inaccurate. 2039 + * Search forward to find the current one. 2040 + */ 2022 2041 while (_prb_read_valid(rb, &seq, NULL, NULL)) 2023 2042 seq++; 2024 2043 ··· 2085 2044 rb->desc_ring.infos = infos; 2086 2045 atomic_long_set(&rb->desc_ring.head_id, DESC0_ID(descbits)); 2087 2046 atomic_long_set(&rb->desc_ring.tail_id, DESC0_ID(descbits)); 2047 + atomic_long_set(&rb->desc_ring.last_finalized_id, DESC0_ID(descbits)); 2088 2048 2089 2049 rb->text_data_ring.size_bits = textbits; 2090 2050 rb->text_data_ring.data = text_buf;
+2
kernel/printk/printk_ringbuffer.h
··· 75 75 struct printk_info *infos; 76 76 atomic_long_t head_id; 77 77 atomic_long_t tail_id; 78 + atomic_long_t last_finalized_id; 78 79 }; 79 80 80 81 /* ··· 259 258 .infos = &_##name##_infos[0], \ 260 259 .head_id = ATOMIC_INIT(DESC0_ID(descbits)), \ 261 260 .tail_id = ATOMIC_INIT(DESC0_ID(descbits)), \ 261 + .last_finalized_id = ATOMIC_INIT(DESC0_ID(descbits)), \ 262 262 }, \ 263 263 .text_data_ring = { \ 264 264 .size_bits = (avgtextbits) + (descbits), \
+29 -20
lib/vsprintf.c
··· 49 49 50 50 #include <asm/page.h> /* for PAGE_SIZE */ 51 51 #include <asm/byteorder.h> /* cpu_to_le16 */ 52 + #include <asm/unaligned.h> 52 53 53 54 #include <linux/string_helpers.h> 54 55 #include "kstrtox.h" 56 + 57 + /* Disable pointer hashing if requested */ 58 + bool no_hash_pointers __ro_after_init; 59 + EXPORT_SYMBOL_GPL(no_hash_pointers); 55 60 56 61 static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base) 57 62 { ··· 855 850 return pointer_string(buf, end, (const void *)hashval, spec); 856 851 } 857 852 853 + static char *default_pointer(char *buf, char *end, const void *ptr, 854 + struct printf_spec spec) 855 + { 856 + /* 857 + * default is to _not_ leak addresses, so hash before printing, 858 + * unless no_hash_pointers is specified on the command line. 859 + */ 860 + if (unlikely(no_hash_pointers)) 861 + return pointer_string(buf, end, ptr, spec); 862 + 863 + return ptr_to_id(buf, end, ptr, spec); 864 + } 865 + 858 866 int kptr_restrict __read_mostly; 859 867 860 868 static noinline_for_stack ··· 877 859 switch (kptr_restrict) { 878 860 case 0: 879 861 /* Handle as %p, hash and do _not_ leak addresses. */ 880 - return ptr_to_id(buf, end, ptr, spec); 862 + return default_pointer(buf, end, ptr, spec); 881 863 case 1: { 882 864 const struct cred *cred; 883 865 ··· 1781 1763 char output[sizeof("0123 little-endian (0x01234567)")]; 1782 1764 char *p = output; 1783 1765 unsigned int i; 1784 - u32 val; 1766 + u32 orig, val; 1785 1767 1786 1768 if (fmt[1] != 'c' || fmt[2] != 'c') 1787 1769 return error_string(buf, end, "(%p4?)", spec); ··· 1789 1771 if (check_pointer(&buf, end, fourcc, spec)) 1790 1772 return buf; 1791 1773 1792 - val = *fourcc & ~BIT(31); 1774 + orig = get_unaligned(fourcc); 1775 + val = orig & ~BIT(31); 1793 1776 1794 - for (i = 0; i < sizeof(*fourcc); i++) { 1777 + for (i = 0; i < sizeof(u32); i++) { 1795 1778 unsigned char c = val >> (i * 8); 1796 1779 1797 1780 /* Print non-control ASCII characters as-is, dot otherwise */ 1798 1781 *p++ = isascii(c) && isprint(c) ? c : '.'; 1799 1782 } 1800 1783 1801 - strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian"); 1784 + *p++ = ' '; 1785 + strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian"); 1802 1786 p += strlen(p); 1803 1787 1804 1788 *p++ = ' '; 1805 1789 *p++ = '('; 1806 - p = special_hex_number(p, output + sizeof(output) - 2, *fourcc, sizeof(u32)); 1790 + p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32)); 1807 1791 *p++ = ')'; 1808 1792 *p = '\0'; 1809 1793 ··· 2245 2225 return widen_string(buf, buf - buf_start, end, spec); 2246 2226 } 2247 2227 2248 - /* Disable pointer hashing if requested */ 2249 - bool no_hash_pointers __ro_after_init; 2250 - EXPORT_SYMBOL_GPL(no_hash_pointers); 2251 - 2252 2228 int __init no_hash_pointers_enable(char *str) 2253 2229 { 2254 2230 if (no_hash_pointers) ··· 2473 2457 case 'e': 2474 2458 /* %pe with a non-ERR_PTR gets treated as plain %p */ 2475 2459 if (!IS_ERR(ptr)) 2476 - break; 2460 + return default_pointer(buf, end, ptr, spec); 2477 2461 return err_ptr(buf, end, ptr, spec); 2478 2462 case 'u': 2479 2463 case 'k': ··· 2483 2467 default: 2484 2468 return error_string(buf, end, "(einval)", spec); 2485 2469 } 2470 + default: 2471 + return default_pointer(buf, end, ptr, spec); 2486 2472 } 2487 - 2488 - /* 2489 - * default is to _not_ leak addresses, so hash before printing, 2490 - * unless no_hash_pointers is specified on the command line. 2491 - */ 2492 - if (unlikely(no_hash_pointers)) 2493 - return pointer_string(buf, end, ptr, spec); 2494 - else 2495 - return ptr_to_id(buf, end, ptr, spec); 2496 2473 } 2497 2474 2498 2475 /*