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

Configure Feed

Select the types of activity you want to include in your feed.

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

Pull printk kernel thread revert from Petr Mladek:
"Revert printk console kthreads.

The testing of 5.19 release candidates revealed issues that did not
happen when all consoles were serialized using the console semaphore.

More time is needed to check expectations of the existing console
drivers and be confident that they can be safely used in parallel"

* tag 'printk-for-5.19-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux:
Revert "printk: add functions to prefer direct printing"
Revert "printk: add kthread console printers"
Revert "printk: extend console_lock for per-console locking"
Revert "printk: remove @console_locked"
Revert "printk: Block console kthreads when direct printing will be required"
Revert "printk: Wait for the global console lock when the system is going down"

+65 -640
-2
drivers/tty/sysrq.c
··· 581 581 582 582 rcu_sysrq_start(); 583 583 rcu_read_lock(); 584 - printk_prefer_direct_enter(); 585 584 /* 586 585 * Raise the apparent loglevel to maximum so that the sysrq header 587 586 * is shown to provide the user with positive feedback. We do not ··· 622 623 pr_cont("\n"); 623 624 console_loglevel = orig_log_level; 624 625 } 625 - printk_prefer_direct_exit(); 626 626 rcu_read_unlock(); 627 627 rcu_sysrq_end(); 628 628
-17
include/linux/console.h
··· 16 16 17 17 #include <linux/atomic.h> 18 18 #include <linux/types.h> 19 - #include <linux/mutex.h> 20 19 21 20 struct vc_data; 22 21 struct console_font_op; ··· 153 154 uint ospeed; 154 155 u64 seq; 155 156 unsigned long dropped; 156 - struct task_struct *thread; 157 - bool blocked; 158 - 159 - /* 160 - * The per-console lock is used by printing kthreads to synchronize 161 - * this console with callers of console_lock(). This is necessary in 162 - * order to allow printing kthreads to run in parallel to each other, 163 - * while each safely accessing the @blocked field and synchronizing 164 - * against direct printing via console_lock/console_unlock. 165 - * 166 - * Note: For synchronizing against direct printing via 167 - * console_trylock/console_unlock, see the static global 168 - * variable @console_kthreads_active. 169 - */ 170 - struct mutex lock; 171 - 172 157 void *data; 173 158 struct console *next; 174 159 };
-16
include/linux/printk.h
··· 169 169 #define printk_deferred_enter __printk_safe_enter 170 170 #define printk_deferred_exit __printk_safe_exit 171 171 172 - extern void printk_prefer_direct_enter(void); 173 - extern void printk_prefer_direct_exit(void); 174 - 175 172 extern bool pr_flush(int timeout_ms, bool reset_on_progress); 176 - extern void try_block_console_kthreads(int timeout_ms); 177 173 178 174 /* 179 175 * Please don't use printk_ratelimit(), because it shares ratelimiting state ··· 221 225 { 222 226 } 223 227 224 - static inline void printk_prefer_direct_enter(void) 225 - { 226 - } 227 - 228 - static inline void printk_prefer_direct_exit(void) 229 - { 230 - } 231 - 232 228 static inline bool pr_flush(int timeout_ms, bool reset_on_progress) 233 229 { 234 230 return true; 235 - } 236 - 237 - static inline void try_block_console_kthreads(int timeout_ms) 238 - { 239 231 } 240 232 241 233 static inline int printk_ratelimit(void)
+1 -10
kernel/hung_task.c
··· 127 127 * complain: 128 128 */ 129 129 if (sysctl_hung_task_warnings) { 130 - printk_prefer_direct_enter(); 131 - 132 130 if (sysctl_hung_task_warnings > 0) 133 131 sysctl_hung_task_warnings--; 134 132 pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n", ··· 142 144 143 145 if (sysctl_hung_task_all_cpu_backtrace) 144 146 hung_task_show_all_bt = true; 145 - 146 - printk_prefer_direct_exit(); 147 147 } 148 148 149 149 touch_nmi_watchdog(); ··· 204 208 } 205 209 unlock: 206 210 rcu_read_unlock(); 207 - if (hung_task_show_lock) { 208 - printk_prefer_direct_enter(); 211 + if (hung_task_show_lock) 209 212 debug_show_all_locks(); 210 - printk_prefer_direct_exit(); 211 - } 212 213 213 214 if (hung_task_show_all_bt) { 214 215 hung_task_show_all_bt = false; 215 - printk_prefer_direct_enter(); 216 216 trigger_all_cpu_backtrace(); 217 - printk_prefer_direct_exit(); 218 217 } 219 218 220 219 if (hung_task_call_panic)
-6
kernel/panic.c
··· 297 297 * unfortunately means it may not be hardened to work in a 298 298 * panic situation. 299 299 */ 300 - try_block_console_kthreads(10000); 301 300 smp_send_stop(); 302 301 } else { 303 302 /* ··· 304 305 * kmsg_dump, we will need architecture dependent extra 305 306 * works in addition to stopping other CPUs. 306 307 */ 307 - try_block_console_kthreads(10000); 308 308 crash_smp_send_stop(); 309 309 } 310 310 ··· 603 605 { 604 606 disable_trace_on_warning(); 605 607 606 - printk_prefer_direct_enter(); 607 - 608 608 if (file) 609 609 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 610 610 raw_smp_processor_id(), current->pid, file, line, ··· 632 636 633 637 /* Just a warning, don't kill lockdep. */ 634 638 add_taint(taint, LOCKDEP_STILL_OK); 635 - 636 - printk_prefer_direct_exit(); 637 639 } 638 640 639 641 #ifndef __WARN_FLAGS
-2
kernel/printk/internal.h
··· 20 20 LOG_CONT = 8, /* text is a fragment of a continuation line */ 21 21 }; 22 22 23 - extern bool block_console_kthreads; 24 - 25 23 __printf(4, 0) 26 24 int vprintk_store(int facility, int level, 27 25 const struct dev_printk_info *dev_info,
+63 -530
kernel/printk/printk.c
··· 224 224 static int nr_ext_console_drivers; 225 225 226 226 /* 227 - * Used to synchronize printing kthreads against direct printing via 228 - * console_trylock/console_unlock. 229 - * 230 - * Values: 231 - * -1 = console kthreads atomically blocked (via global trylock) 232 - * 0 = no kthread printing, console not locked (via trylock) 233 - * >0 = kthread(s) actively printing 234 - * 235 - * Note: For synchronizing against direct printing via 236 - * console_lock/console_unlock, see the @lock variable in 237 - * struct console. 238 - */ 239 - static atomic_t console_kthreads_active = ATOMIC_INIT(0); 240 - 241 - #define console_kthreads_atomic_tryblock() \ 242 - (atomic_cmpxchg(&console_kthreads_active, 0, -1) == 0) 243 - #define console_kthreads_atomic_unblock() \ 244 - atomic_cmpxchg(&console_kthreads_active, -1, 0) 245 - #define console_kthreads_atomically_blocked() \ 246 - (atomic_read(&console_kthreads_active) == -1) 247 - 248 - #define console_kthread_printing_tryenter() \ 249 - atomic_inc_unless_negative(&console_kthreads_active) 250 - #define console_kthread_printing_exit() \ 251 - atomic_dec(&console_kthreads_active) 252 - 253 - /* Block console kthreads to avoid processing new messages. */ 254 - bool block_console_kthreads; 255 - 256 - /* 257 227 * Helper macros to handle lockdep when locking/unlocking console_sem. We use 258 228 * macros instead of functions so that _RET_IP_ contains useful information. 259 229 */ ··· 271 301 } 272 302 273 303 /* 274 - * Tracks whether kthread printers are all blocked. A value of true implies 275 - * that the console is locked via console_lock() or the console is suspended. 276 - * Writing to this variable requires holding @console_sem. 304 + * This is used for debugging the mess that is the VT code by 305 + * keeping track if we have the console semaphore held. It's 306 + * definitely not the perfect debug tool (we don't know if _WE_ 307 + * hold it and are racing, but it helps tracking those weird code 308 + * paths in the console code where we end up in places I want 309 + * locked without the console semaphore held). 277 310 */ 278 - static bool console_kthreads_blocked; 279 - 280 - /* 281 - * Block all kthread printers from a schedulable context. 282 - * 283 - * Requires holding @console_sem. 284 - */ 285 - static void console_kthreads_block(void) 286 - { 287 - struct console *con; 288 - 289 - for_each_console(con) { 290 - mutex_lock(&con->lock); 291 - con->blocked = true; 292 - mutex_unlock(&con->lock); 293 - } 294 - 295 - console_kthreads_blocked = true; 296 - } 297 - 298 - /* 299 - * Unblock all kthread printers from a schedulable context. 300 - * 301 - * Requires holding @console_sem. 302 - */ 303 - static void console_kthreads_unblock(void) 304 - { 305 - struct console *con; 306 - 307 - for_each_console(con) { 308 - mutex_lock(&con->lock); 309 - con->blocked = false; 310 - mutex_unlock(&con->lock); 311 - } 312 - 313 - console_kthreads_blocked = false; 314 - } 315 - 316 - static int console_suspended; 311 + static int console_locked, console_suspended; 317 312 318 313 /* 319 314 * Array of consoles built from command line options (console=) ··· 361 426 /* syslog_lock protects syslog_* variables and write access to clear_seq. */ 362 427 static DEFINE_MUTEX(syslog_lock); 363 428 364 - /* 365 - * A flag to signify if printk_activate_kthreads() has already started the 366 - * kthread printers. If true, any later registered consoles must start their 367 - * own kthread directly. The flag is write protected by the console_lock. 368 - */ 369 - static bool printk_kthreads_available; 370 - 371 429 #ifdef CONFIG_PRINTK 372 - static atomic_t printk_prefer_direct = ATOMIC_INIT(0); 373 - 374 - /** 375 - * printk_prefer_direct_enter - cause printk() calls to attempt direct 376 - * printing to all enabled consoles 377 - * 378 - * Since it is not possible to call into the console printing code from any 379 - * context, there is no guarantee that direct printing will occur. 380 - * 381 - * This globally effects all printk() callers. 382 - * 383 - * Context: Any context. 384 - */ 385 - void printk_prefer_direct_enter(void) 386 - { 387 - atomic_inc(&printk_prefer_direct); 388 - } 389 - 390 - /** 391 - * printk_prefer_direct_exit - restore printk() behavior 392 - * 393 - * Context: Any context. 394 - */ 395 - void printk_prefer_direct_exit(void) 396 - { 397 - WARN_ON(atomic_dec_if_positive(&printk_prefer_direct) < 0); 398 - } 399 - 400 - /* 401 - * Calling printk() always wakes kthread printers so that they can 402 - * flush the new message to their respective consoles. Also, if direct 403 - * printing is allowed, printk() tries to flush the messages directly. 404 - * 405 - * Direct printing is allowed in situations when the kthreads 406 - * are not available or the system is in a problematic state. 407 - * 408 - * See the implementation about possible races. 409 - */ 410 - static inline bool allow_direct_printing(void) 411 - { 412 - /* 413 - * Checking kthread availability is a possible race because the 414 - * kthread printers can become permanently disabled during runtime. 415 - * However, doing that requires holding the console_lock, so any 416 - * pending messages will be direct printed by console_unlock(). 417 - */ 418 - if (!printk_kthreads_available) 419 - return true; 420 - 421 - /* 422 - * Prefer direct printing when the system is in a problematic state. 423 - * The context that sets this state will always see the updated value. 424 - * The other contexts do not care. Anyway, direct printing is just a 425 - * best effort. The direct output is only possible when console_lock 426 - * is not already taken and no kthread printers are actively printing. 427 - */ 428 - return (system_state > SYSTEM_RUNNING || 429 - oops_in_progress || 430 - atomic_read(&printk_prefer_direct)); 431 - } 432 - 433 430 DECLARE_WAIT_QUEUE_HEAD(log_wait); 434 431 /* All 3 protected by @syslog_lock. */ 435 432 /* the next printk record to read by syslog(READ) or /proc/kmsg */ ··· 2252 2385 printed_len = vprintk_store(facility, level, dev_info, fmt, args); 2253 2386 2254 2387 /* If called from the scheduler, we can not call up(). */ 2255 - if (!in_sched && allow_direct_printing()) { 2388 + if (!in_sched) { 2256 2389 /* 2257 2390 * The caller may be holding system-critical or 2258 - * timing-sensitive locks. Disable preemption during direct 2391 + * timing-sensitive locks. Disable preemption during 2259 2392 * printing of all remaining records to all consoles so that 2260 2393 * this context can return as soon as possible. Hopefully 2261 2394 * another printk() caller will take over the printing. ··· 2298 2431 2299 2432 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress); 2300 2433 2301 - static void printk_start_kthread(struct console *con); 2302 - 2303 2434 #else /* CONFIG_PRINTK */ 2304 2435 2305 2436 #define CONSOLE_LOG_MAX 0 ··· 2331 2466 } 2332 2467 static bool suppress_message_printing(int level) { return false; } 2333 2468 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; } 2334 - static void printk_start_kthread(struct console *con) { } 2335 - static bool allow_direct_printing(void) { return true; } 2336 2469 2337 2470 #endif /* CONFIG_PRINTK */ 2338 2471 ··· 2549 2686 /* If trylock fails, someone else is doing the printing */ 2550 2687 if (console_trylock()) 2551 2688 console_unlock(); 2552 - else { 2553 - /* 2554 - * If a new CPU comes online, the conditions for 2555 - * printer_should_wake() may have changed for some 2556 - * kthread printer with !CON_ANYTIME. 2557 - */ 2558 - wake_up_klogd(); 2559 - } 2560 2689 } 2561 2690 return 0; 2562 2691 } ··· 2568 2713 down_console_sem(); 2569 2714 if (console_suspended) 2570 2715 return; 2571 - console_kthreads_block(); 2716 + console_locked = 1; 2572 2717 console_may_schedule = 1; 2573 2718 } 2574 2719 EXPORT_SYMBOL(console_lock); ··· 2589 2734 up_console_sem(); 2590 2735 return 0; 2591 2736 } 2592 - if (!console_kthreads_atomic_tryblock()) { 2593 - up_console_sem(); 2594 - return 0; 2595 - } 2737 + console_locked = 1; 2596 2738 console_may_schedule = 0; 2597 2739 return 1; 2598 2740 } 2599 2741 EXPORT_SYMBOL(console_trylock); 2600 2742 2601 - /* 2602 - * This is used to help to make sure that certain paths within the VT code are 2603 - * running with the console lock held. It is definitely not the perfect debug 2604 - * tool (it is not known if the VT code is the task holding the console lock), 2605 - * but it helps tracking those weird code paths in the console code such as 2606 - * when the console is suspended: where the console is not locked but no 2607 - * console printing may occur. 2608 - * 2609 - * Note: This returns true when the console is suspended but is not locked. 2610 - * This is intentional because the VT code must consider that situation 2611 - * the same as if the console was locked. 2612 - */ 2613 2743 int is_console_locked(void) 2614 2744 { 2615 - return (console_kthreads_blocked || atomic_read(&console_kthreads_active)); 2745 + return console_locked; 2616 2746 } 2617 2747 EXPORT_SYMBOL(is_console_locked); 2618 2748 ··· 2620 2780 return atomic_read(&panic_cpu) != raw_smp_processor_id(); 2621 2781 } 2622 2782 2623 - static inline bool __console_is_usable(short flags) 2783 + /* 2784 + * Check if the given console is currently capable and allowed to print 2785 + * records. 2786 + * 2787 + * Requires the console_lock. 2788 + */ 2789 + static inline bool console_is_usable(struct console *con) 2624 2790 { 2625 - if (!(flags & CON_ENABLED)) 2791 + if (!(con->flags & CON_ENABLED)) 2792 + return false; 2793 + 2794 + if (!con->write) 2626 2795 return false; 2627 2796 2628 2797 /* ··· 2640 2791 * cope (CON_ANYTIME) don't call them until this CPU is officially up. 2641 2792 */ 2642 2793 if (!cpu_online(raw_smp_processor_id()) && 2643 - !(flags & CON_ANYTIME)) 2794 + !(con->flags & CON_ANYTIME)) 2644 2795 return false; 2645 2796 2646 2797 return true; 2647 2798 } 2648 2799 2649 - /* 2650 - * Check if the given console is currently capable and allowed to print 2651 - * records. 2652 - * 2653 - * Requires holding the console_lock. 2654 - */ 2655 - static inline bool console_is_usable(struct console *con) 2656 - { 2657 - if (!con->write) 2658 - return false; 2659 - 2660 - return __console_is_usable(con->flags); 2661 - } 2662 - 2663 2800 static void __console_unlock(void) 2664 2801 { 2665 - /* 2666 - * Depending on whether console_lock() or console_trylock() was used, 2667 - * appropriately allow the kthread printers to continue. 2668 - */ 2669 - if (console_kthreads_blocked) 2670 - console_kthreads_unblock(); 2671 - else 2672 - console_kthreads_atomic_unblock(); 2673 - 2674 - /* 2675 - * New records may have arrived while the console was locked. 2676 - * Wake the kthread printers to print them. 2677 - */ 2678 - wake_up_klogd(); 2679 - 2802 + console_locked = 0; 2680 2803 up_console_sem(); 2681 2804 } 2682 2805 ··· 2666 2845 * 2667 2846 * @handover will be set to true if a printk waiter has taken over the 2668 2847 * console_lock, in which case the caller is no longer holding the 2669 - * console_lock. Otherwise it is set to false. A NULL pointer may be provided 2670 - * to disable allowing the console_lock to be taken over by a printk waiter. 2848 + * console_lock. Otherwise it is set to false. 2671 2849 * 2672 2850 * Returns false if the given console has no next record to print, otherwise 2673 2851 * true. 2674 2852 * 2675 - * Requires the console_lock if @handover is non-NULL. 2676 - * Requires con->lock otherwise. 2853 + * Requires the console_lock. 2677 2854 */ 2678 - static bool __console_emit_next_record(struct console *con, char *text, char *ext_text, 2679 - char *dropped_text, bool *handover) 2855 + static bool console_emit_next_record(struct console *con, char *text, char *ext_text, 2856 + char *dropped_text, bool *handover) 2680 2857 { 2681 - static atomic_t panic_console_dropped = ATOMIC_INIT(0); 2858 + static int panic_console_dropped; 2682 2859 struct printk_info info; 2683 2860 struct printk_record r; 2684 2861 unsigned long flags; ··· 2685 2866 2686 2867 prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); 2687 2868 2688 - if (handover) 2689 - *handover = false; 2869 + *handover = false; 2690 2870 2691 2871 if (!prb_read_valid(prb, con->seq, &r)) 2692 2872 return false; ··· 2693 2875 if (con->seq != r.info->seq) { 2694 2876 con->dropped += r.info->seq - con->seq; 2695 2877 con->seq = r.info->seq; 2696 - if (panic_in_progress() && 2697 - atomic_fetch_inc_relaxed(&panic_console_dropped) > 10) { 2878 + if (panic_in_progress() && panic_console_dropped++ > 10) { 2698 2879 suppress_panic_printk = 1; 2699 2880 pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); 2700 2881 } ··· 2715 2898 len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time); 2716 2899 } 2717 2900 2718 - if (handover) { 2719 - /* 2720 - * While actively printing out messages, if another printk() 2721 - * were to occur on another CPU, it may wait for this one to 2722 - * finish. This task can not be preempted if there is a 2723 - * waiter waiting to take over. 2724 - * 2725 - * Interrupts are disabled because the hand over to a waiter 2726 - * must not be interrupted until the hand over is completed 2727 - * (@console_waiter is cleared). 2728 - */ 2729 - printk_safe_enter_irqsave(flags); 2730 - console_lock_spinning_enable(); 2901 + /* 2902 + * While actively printing out messages, if another printk() 2903 + * were to occur on another CPU, it may wait for this one to 2904 + * finish. This task can not be preempted if there is a 2905 + * waiter waiting to take over. 2906 + * 2907 + * Interrupts are disabled because the hand over to a waiter 2908 + * must not be interrupted until the hand over is completed 2909 + * (@console_waiter is cleared). 2910 + */ 2911 + printk_safe_enter_irqsave(flags); 2912 + console_lock_spinning_enable(); 2731 2913 2732 - /* don't trace irqsoff print latency */ 2733 - stop_critical_timings(); 2734 - } 2735 - 2914 + stop_critical_timings(); /* don't trace print latency */ 2736 2915 call_console_driver(con, write_text, len, dropped_text); 2916 + start_critical_timings(); 2737 2917 2738 2918 con->seq++; 2739 2919 2740 - if (handover) { 2741 - start_critical_timings(); 2742 - *handover = console_lock_spinning_disable_and_check(); 2743 - printk_safe_exit_irqrestore(flags); 2744 - } 2920 + *handover = console_lock_spinning_disable_and_check(); 2921 + printk_safe_exit_irqrestore(flags); 2745 2922 skip: 2746 2923 return true; 2747 - } 2748 - 2749 - /* 2750 - * Print a record for a given console, but allow another printk() caller to 2751 - * take over the console_lock and continue printing. 2752 - * 2753 - * Requires the console_lock, but depending on @handover after the call, the 2754 - * caller may no longer have the console_lock. 2755 - * 2756 - * See __console_emit_next_record() for argument and return details. 2757 - */ 2758 - static bool console_emit_next_record_transferable(struct console *con, char *text, char *ext_text, 2759 - char *dropped_text, bool *handover) 2760 - { 2761 - /* 2762 - * Handovers are only supported if threaded printers are atomically 2763 - * blocked. The context taking over the console_lock may be atomic. 2764 - */ 2765 - if (!console_kthreads_atomically_blocked()) { 2766 - *handover = false; 2767 - handover = NULL; 2768 - } 2769 - 2770 - return __console_emit_next_record(con, text, ext_text, dropped_text, handover); 2771 2924 } 2772 2925 2773 2926 /* ··· 2758 2971 * were flushed to all usable consoles. A returned false informs the caller 2759 2972 * that everything was not flushed (either there were no usable consoles or 2760 2973 * another context has taken over printing or it is a panic situation and this 2761 - * is not the panic CPU or direct printing is not preferred). Regardless the 2762 - * reason, the caller should assume it is not useful to immediately try again. 2974 + * is not the panic CPU). Regardless the reason, the caller should assume it 2975 + * is not useful to immediately try again. 2763 2976 * 2764 2977 * Requires the console_lock. 2765 2978 */ ··· 2776 2989 *handover = false; 2777 2990 2778 2991 do { 2779 - /* Let the kthread printers do the work if they can. */ 2780 - if (!allow_direct_printing()) 2781 - return false; 2782 - 2783 2992 any_progress = false; 2784 2993 2785 2994 for_each_console(con) { ··· 2787 3004 2788 3005 if (con->flags & CON_EXTENDED) { 2789 3006 /* Extended consoles do not print "dropped messages". */ 2790 - progress = console_emit_next_record_transferable(con, &text[0], 2791 - &ext_text[0], NULL, handover); 3007 + progress = console_emit_next_record(con, &text[0], 3008 + &ext_text[0], NULL, 3009 + handover); 2792 3010 } else { 2793 - progress = console_emit_next_record_transferable(con, &text[0], 2794 - NULL, &dropped_text[0], handover); 3011 + progress = console_emit_next_record(con, &text[0], 3012 + NULL, &dropped_text[0], 3013 + handover); 2795 3014 } 2796 3015 if (*handover) 2797 3016 return false; ··· 2908 3123 if (oops_in_progress) { 2909 3124 if (down_trylock_console_sem() != 0) 2910 3125 return; 2911 - if (!console_kthreads_atomic_tryblock()) { 2912 - up_console_sem(); 2913 - return; 2914 - } 2915 3126 } else 2916 3127 console_lock(); 2917 3128 3129 + console_locked = 1; 2918 3130 console_may_schedule = 0; 2919 3131 for_each_console(c) 2920 3132 if ((c->flags & CON_ENABLED) && c->unblank) ··· 3190 3408 nr_ext_console_drivers++; 3191 3409 3192 3410 newcon->dropped = 0; 3193 - newcon->thread = NULL; 3194 - newcon->blocked = true; 3195 - mutex_init(&newcon->lock); 3196 - 3197 3411 if (newcon->flags & CON_PRINTBUFFER) { 3198 3412 /* Get a consistent copy of @syslog_seq. */ 3199 3413 mutex_lock(&syslog_lock); ··· 3199 3421 /* Begin with next message. */ 3200 3422 newcon->seq = prb_next_seq(prb); 3201 3423 } 3202 - 3203 - if (printk_kthreads_available) 3204 - printk_start_kthread(newcon); 3205 - 3206 3424 console_unlock(); 3207 3425 console_sysfs_notify(); 3208 3426 ··· 3225 3451 3226 3452 int unregister_console(struct console *console) 3227 3453 { 3228 - struct task_struct *thd; 3229 3454 struct console *con; 3230 3455 int res; 3231 3456 ··· 3265 3492 console_drivers->flags |= CON_CONSDEV; 3266 3493 3267 3494 console->flags &= ~CON_ENABLED; 3268 - 3269 - /* 3270 - * console->thread can only be cleared under the console lock. But 3271 - * stopping the thread must be done without the console lock. The 3272 - * task that clears @thread is the task that stops the kthread. 3273 - */ 3274 - thd = console->thread; 3275 - console->thread = NULL; 3276 - 3277 3495 console_unlock(); 3278 - 3279 - if (thd) 3280 - kthread_stop(thd); 3281 - 3282 3496 console_sysfs_notify(); 3283 3497 3284 3498 if (console->exit) ··· 3361 3601 } 3362 3602 late_initcall(printk_late_init); 3363 3603 3364 - static int __init printk_activate_kthreads(void) 3365 - { 3366 - struct console *con; 3367 - 3368 - console_lock(); 3369 - printk_kthreads_available = true; 3370 - for_each_console(con) 3371 - printk_start_kthread(con); 3372 - console_unlock(); 3373 - 3374 - return 0; 3375 - } 3376 - early_initcall(printk_activate_kthreads); 3377 - 3378 3604 #if defined CONFIG_PRINTK 3379 3605 /* If @con is specified, only wait for that console. Otherwise wait for all. */ 3380 3606 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) ··· 3435 3689 } 3436 3690 EXPORT_SYMBOL(pr_flush); 3437 3691 3438 - static void __printk_fallback_preferred_direct(void) 3439 - { 3440 - printk_prefer_direct_enter(); 3441 - pr_err("falling back to preferred direct printing\n"); 3442 - printk_kthreads_available = false; 3443 - } 3444 - 3445 - /* 3446 - * Enter preferred direct printing, but never exit. Mark console threads as 3447 - * unavailable. The system is then forever in preferred direct printing and 3448 - * any printing threads will exit. 3449 - * 3450 - * Must *not* be called under console_lock. Use 3451 - * __printk_fallback_preferred_direct() if already holding console_lock. 3452 - */ 3453 - static void printk_fallback_preferred_direct(void) 3454 - { 3455 - console_lock(); 3456 - __printk_fallback_preferred_direct(); 3457 - console_unlock(); 3458 - } 3459 - 3460 - /* 3461 - * Print a record for a given console, not allowing another printk() caller 3462 - * to take over. This is appropriate for contexts that do not have the 3463 - * console_lock. 3464 - * 3465 - * See __console_emit_next_record() for argument and return details. 3466 - */ 3467 - static bool console_emit_next_record(struct console *con, char *text, char *ext_text, 3468 - char *dropped_text) 3469 - { 3470 - return __console_emit_next_record(con, text, ext_text, dropped_text, NULL); 3471 - } 3472 - 3473 - static bool printer_should_wake(struct console *con, u64 seq) 3474 - { 3475 - short flags; 3476 - 3477 - if (kthread_should_stop() || !printk_kthreads_available) 3478 - return true; 3479 - 3480 - if (con->blocked || 3481 - console_kthreads_atomically_blocked() || 3482 - block_console_kthreads || 3483 - system_state > SYSTEM_RUNNING || 3484 - oops_in_progress) { 3485 - return false; 3486 - } 3487 - 3488 - /* 3489 - * This is an unsafe read from con->flags, but a false positive is 3490 - * not a problem. Worst case it would allow the printer to wake up 3491 - * although it is disabled. But the printer will notice that when 3492 - * attempting to print and instead go back to sleep. 3493 - */ 3494 - flags = data_race(READ_ONCE(con->flags)); 3495 - 3496 - if (!__console_is_usable(flags)) 3497 - return false; 3498 - 3499 - return prb_read_valid(prb, seq, NULL); 3500 - } 3501 - 3502 - static int printk_kthread_func(void *data) 3503 - { 3504 - struct console *con = data; 3505 - char *dropped_text = NULL; 3506 - char *ext_text = NULL; 3507 - u64 seq = 0; 3508 - char *text; 3509 - int error; 3510 - 3511 - text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL); 3512 - if (!text) { 3513 - con_printk(KERN_ERR, con, "failed to allocate text buffer\n"); 3514 - printk_fallback_preferred_direct(); 3515 - goto out; 3516 - } 3517 - 3518 - if (con->flags & CON_EXTENDED) { 3519 - ext_text = kmalloc(CONSOLE_EXT_LOG_MAX, GFP_KERNEL); 3520 - if (!ext_text) { 3521 - con_printk(KERN_ERR, con, "failed to allocate ext_text buffer\n"); 3522 - printk_fallback_preferred_direct(); 3523 - goto out; 3524 - } 3525 - } else { 3526 - dropped_text = kmalloc(DROPPED_TEXT_MAX, GFP_KERNEL); 3527 - if (!dropped_text) { 3528 - con_printk(KERN_ERR, con, "failed to allocate dropped_text buffer\n"); 3529 - printk_fallback_preferred_direct(); 3530 - goto out; 3531 - } 3532 - } 3533 - 3534 - con_printk(KERN_INFO, con, "printing thread started\n"); 3535 - 3536 - for (;;) { 3537 - /* 3538 - * Guarantee this task is visible on the waitqueue before 3539 - * checking the wake condition. 3540 - * 3541 - * The full memory barrier within set_current_state() of 3542 - * prepare_to_wait_event() pairs with the full memory barrier 3543 - * within wq_has_sleeper(). 3544 - * 3545 - * This pairs with __wake_up_klogd:A. 3546 - */ 3547 - error = wait_event_interruptible(log_wait, 3548 - printer_should_wake(con, seq)); /* LMM(printk_kthread_func:A) */ 3549 - 3550 - if (kthread_should_stop() || !printk_kthreads_available) 3551 - break; 3552 - 3553 - if (error) 3554 - continue; 3555 - 3556 - error = mutex_lock_interruptible(&con->lock); 3557 - if (error) 3558 - continue; 3559 - 3560 - if (con->blocked || 3561 - !console_kthread_printing_tryenter()) { 3562 - /* Another context has locked the console_lock. */ 3563 - mutex_unlock(&con->lock); 3564 - continue; 3565 - } 3566 - 3567 - /* 3568 - * Although this context has not locked the console_lock, it 3569 - * is known that the console_lock is not locked and it is not 3570 - * possible for any other context to lock the console_lock. 3571 - * Therefore it is safe to read con->flags. 3572 - */ 3573 - 3574 - if (!__console_is_usable(con->flags)) { 3575 - console_kthread_printing_exit(); 3576 - mutex_unlock(&con->lock); 3577 - continue; 3578 - } 3579 - 3580 - /* 3581 - * Even though the printk kthread is always preemptible, it is 3582 - * still not allowed to call cond_resched() from within 3583 - * console drivers. The task may become non-preemptible in the 3584 - * console driver call chain. For example, vt_console_print() 3585 - * takes a spinlock and then can call into fbcon_redraw(), 3586 - * which can conditionally invoke cond_resched(). 3587 - */ 3588 - console_may_schedule = 0; 3589 - console_emit_next_record(con, text, ext_text, dropped_text); 3590 - 3591 - seq = con->seq; 3592 - 3593 - console_kthread_printing_exit(); 3594 - 3595 - mutex_unlock(&con->lock); 3596 - } 3597 - 3598 - con_printk(KERN_INFO, con, "printing thread stopped\n"); 3599 - out: 3600 - kfree(dropped_text); 3601 - kfree(ext_text); 3602 - kfree(text); 3603 - 3604 - console_lock(); 3605 - /* 3606 - * If this kthread is being stopped by another task, con->thread will 3607 - * already be NULL. That is fine. The important thing is that it is 3608 - * NULL after the kthread exits. 3609 - */ 3610 - con->thread = NULL; 3611 - console_unlock(); 3612 - 3613 - return 0; 3614 - } 3615 - 3616 - /* Must be called under console_lock. */ 3617 - static void printk_start_kthread(struct console *con) 3618 - { 3619 - /* 3620 - * Do not start a kthread if there is no write() callback. The 3621 - * kthreads assume the write() callback exists. 3622 - */ 3623 - if (!con->write) 3624 - return; 3625 - 3626 - con->thread = kthread_run(printk_kthread_func, con, 3627 - "pr/%s%d", con->name, con->index); 3628 - if (IS_ERR(con->thread)) { 3629 - con->thread = NULL; 3630 - con_printk(KERN_ERR, con, "unable to start printing thread\n"); 3631 - __printk_fallback_preferred_direct(); 3632 - return; 3633 - } 3634 - } 3635 - 3636 3692 /* 3637 3693 * Delayed printk version, for scheduler-internal messages: 3638 3694 */ 3639 - #define PRINTK_PENDING_WAKEUP 0x01 3640 - #define PRINTK_PENDING_DIRECT_OUTPUT 0x02 3695 + #define PRINTK_PENDING_WAKEUP 0x01 3696 + #define PRINTK_PENDING_OUTPUT 0x02 3641 3697 3642 3698 static DEFINE_PER_CPU(int, printk_pending); 3643 3699 ··· 3447 3899 { 3448 3900 int pending = this_cpu_xchg(printk_pending, 0); 3449 3901 3450 - if (pending & PRINTK_PENDING_DIRECT_OUTPUT) { 3451 - printk_prefer_direct_enter(); 3452 - 3902 + if (pending & PRINTK_PENDING_OUTPUT) { 3453 3903 /* If trylock fails, someone else is doing the printing */ 3454 3904 if (console_trylock()) 3455 3905 console_unlock(); 3456 - 3457 - printk_prefer_direct_exit(); 3458 3906 } 3459 3907 3460 3908 if (pending & PRINTK_PENDING_WAKEUP) ··· 3475 3931 * prepare_to_wait_event(), which is called after ___wait_event() adds 3476 3932 * the waiter but before it has checked the wait condition. 3477 3933 * 3478 - * This pairs with devkmsg_read:A, syslog_print:A, and 3479 - * printk_kthread_func:A. 3934 + * This pairs with devkmsg_read:A and syslog_print:A. 3480 3935 */ 3481 3936 if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */ 3482 - (val & PRINTK_PENDING_DIRECT_OUTPUT)) { 3937 + (val & PRINTK_PENDING_OUTPUT)) { 3483 3938 this_cpu_or(printk_pending, val); 3484 3939 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); 3485 3940 } ··· 3496 3953 * New messages may have been added directly to the ringbuffer 3497 3954 * using vprintk_store(), so wake any waiters as well. 3498 3955 */ 3499 - int val = PRINTK_PENDING_WAKEUP; 3500 - 3501 - /* 3502 - * Make sure that some context will print the messages when direct 3503 - * printing is allowed. This happens in situations when the kthreads 3504 - * may not be as reliable or perhaps unusable. 3505 - */ 3506 - if (allow_direct_printing()) 3507 - val |= PRINTK_PENDING_DIRECT_OUTPUT; 3508 - 3509 - __wake_up_klogd(val); 3956 + __wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT); 3510 3957 } 3511 3958 3512 3959 void printk_trigger_flush(void)
-32
kernel/printk/printk_safe.c
··· 8 8 #include <linux/smp.h> 9 9 #include <linux/cpumask.h> 10 10 #include <linux/printk.h> 11 - #include <linux/console.h> 12 11 #include <linux/kprobes.h> 13 - #include <linux/delay.h> 14 12 15 13 #include "internal.h" 16 14 ··· 50 52 return vprintk_default(fmt, args); 51 53 } 52 54 EXPORT_SYMBOL(vprintk); 53 - 54 - /** 55 - * try_block_console_kthreads() - Try to block console kthreads and 56 - * make the global console_lock() avaialble 57 - * 58 - * @timeout_ms: The maximum time (in ms) to wait. 59 - * 60 - * Prevent console kthreads from starting processing new messages. Wait 61 - * until the global console_lock() become available. 62 - * 63 - * Context: Can be called in any context. 64 - */ 65 - void try_block_console_kthreads(int timeout_ms) 66 - { 67 - block_console_kthreads = true; 68 - 69 - /* Do not wait when the console lock could not be safely taken. */ 70 - if (this_cpu_read(printk_context) || in_nmi()) 71 - return; 72 - 73 - while (timeout_ms > 0) { 74 - if (console_trylock()) { 75 - console_unlock(); 76 - return; 77 - } 78 - 79 - udelay(1000); 80 - timeout_ms -= 1; 81 - } 82 - }
-2
kernel/rcu/tree_stall.h
··· 647 647 * See Documentation/RCU/stallwarn.rst for info on how to debug 648 648 * RCU CPU stall warnings. 649 649 */ 650 - printk_prefer_direct_enter(); 651 650 trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected")); 652 651 pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name); 653 652 raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags); ··· 684 685 */ 685 686 set_tsk_need_resched(current); 686 687 set_preempt_need_resched(); 687 - printk_prefer_direct_exit(); 688 688 } 689 689 690 690 static void check_cpu_stall(struct rcu_data *rdp)
+1 -15
kernel/reboot.c
··· 82 82 { 83 83 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); 84 84 system_state = SYSTEM_RESTART; 85 - try_block_console_kthreads(10000); 86 85 usermodehelper_disable(); 87 86 device_shutdown(); 88 87 } ··· 270 271 blocking_notifier_call_chain(&reboot_notifier_list, 271 272 (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); 272 273 system_state = state; 273 - try_block_console_kthreads(10000); 274 274 usermodehelper_disable(); 275 275 device_shutdown(); 276 276 } ··· 819 821 ret = run_cmd(reboot_cmd); 820 822 821 823 if (ret) { 822 - printk_prefer_direct_enter(); 823 824 pr_warn("Failed to start orderly reboot: forcing the issue\n"); 824 825 emergency_sync(); 825 826 kernel_restart(NULL); 826 - printk_prefer_direct_exit(); 827 827 } 828 828 829 829 return ret; ··· 834 838 ret = run_cmd(poweroff_cmd); 835 839 836 840 if (ret && force) { 837 - printk_prefer_direct_enter(); 838 841 pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 839 842 840 843 /* ··· 843 848 */ 844 849 emergency_sync(); 845 850 kernel_power_off(); 846 - printk_prefer_direct_exit(); 847 851 } 848 852 849 853 return ret; ··· 900 906 */ 901 907 static void hw_failure_emergency_poweroff_func(struct work_struct *work) 902 908 { 903 - printk_prefer_direct_enter(); 904 - 905 909 /* 906 910 * We have reached here after the emergency shutdown waiting period has 907 911 * expired. This means orderly_poweroff has not been able to shut off ··· 916 924 */ 917 925 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n"); 918 926 emergency_restart(); 919 - 920 - printk_prefer_direct_exit(); 921 927 } 922 928 923 929 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, ··· 954 964 { 955 965 static atomic_t allow_proceed = ATOMIC_INIT(1); 956 966 957 - printk_prefer_direct_enter(); 958 - 959 967 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); 960 968 961 969 /* Shutdown should be initiated only once. */ 962 970 if (!atomic_dec_and_test(&allow_proceed)) 963 - goto out; 971 + return; 964 972 965 973 /* 966 974 * Queue a backup emergency shutdown in the event of ··· 966 978 */ 967 979 hw_failure_emergency_poweroff(ms_until_forced); 968 980 orderly_poweroff(true); 969 - out: 970 - printk_prefer_direct_exit(); 971 981 } 972 982 EXPORT_SYMBOL_GPL(hw_protection_shutdown); 973 983
-4
kernel/watchdog.c
··· 424 424 /* Start period for the next softlockup warning. */ 425 425 update_report_ts(); 426 426 427 - printk_prefer_direct_enter(); 428 - 429 427 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", 430 428 smp_processor_id(), duration, 431 429 current->comm, task_pid_nr(current)); ··· 442 444 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); 443 445 if (softlockup_panic) 444 446 panic("softlockup: hung tasks"); 445 - 446 - printk_prefer_direct_exit(); 447 447 } 448 448 449 449 return HRTIMER_RESTART;
-4
kernel/watchdog_hld.c
··· 135 135 if (__this_cpu_read(hard_watchdog_warn) == true) 136 136 return; 137 137 138 - printk_prefer_direct_enter(); 139 - 140 138 pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", 141 139 this_cpu); 142 140 print_modules(); ··· 154 156 155 157 if (hardlockup_panic) 156 158 nmi_panic(regs, "Hard LOCKUP"); 157 - 158 - printk_prefer_direct_exit(); 159 159 160 160 __this_cpu_write(hard_watchdog_warn, true); 161 161 return;