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.

Revert "printk: add kthread console printers"

This reverts commit 09c5ba0aa2fcfdadb17d045c3ee6f86d69270df7.

This reverts commit b87f02307d3cfbda768520f0687c51ca77e14fc3.

The testing of 5.19 release candidates revealed missing synchronization
between early and regular console functionality.

It would be possible to start the console kthreads later as a workaround.
But it is clear that console lock serialized console drivers between
each other. It opens a big area of possible problems that were not
considered by people involved in the development and review.

printk() is crucial for debugging kernel issues and console output is
very important part of it. The number of consoles is huge and a proper
review would take some time. As a result it need to be reverted for 5.19.

Link: https://lore.kernel.org/r/YrBdjVwBOVgLfHyb@alley
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20220623145157.21938-6-pmladek@suse.com

+23 -310
-2
include/linux/console.h
··· 153 153 uint ospeed; 154 154 u64 seq; 155 155 unsigned long dropped; 156 - struct task_struct *thread; 157 - 158 156 void *data; 159 157 struct console *next; 160 158 };
+23 -308
kernel/printk/printk.c
··· 361 361 /* syslog_lock protects syslog_* variables and write access to clear_seq. */ 362 362 static DEFINE_MUTEX(syslog_lock); 363 363 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 364 #ifdef CONFIG_PRINTK 372 365 static atomic_t printk_prefer_direct = ATOMIC_INIT(0); 373 366 ··· 388 395 void printk_prefer_direct_exit(void) 389 396 { 390 397 WARN_ON(atomic_dec_if_positive(&printk_prefer_direct) < 0); 391 - } 392 - 393 - /* 394 - * Calling printk() always wakes kthread printers so that they can 395 - * flush the new message to their respective consoles. Also, if direct 396 - * printing is allowed, printk() tries to flush the messages directly. 397 - * 398 - * Direct printing is allowed in situations when the kthreads 399 - * are not available or the system is in a problematic state. 400 - * 401 - * See the implementation about possible races. 402 - */ 403 - static inline bool allow_direct_printing(void) 404 - { 405 - /* 406 - * Checking kthread availability is a possible race because the 407 - * kthread printers can become permanently disabled during runtime. 408 - * However, doing that requires holding the console_lock, so any 409 - * pending messages will be direct printed by console_unlock(). 410 - */ 411 - if (!printk_kthreads_available) 412 - return true; 413 - 414 - /* 415 - * Prefer direct printing when the system is in a problematic state. 416 - * The context that sets this state will always see the updated value. 417 - * The other contexts do not care. Anyway, direct printing is just a 418 - * best effort. The direct output is only possible when console_lock 419 - * is not already taken and no kthread printers are actively printing. 420 - */ 421 - return (system_state > SYSTEM_RUNNING || 422 - oops_in_progress || 423 - atomic_read(&printk_prefer_direct)); 424 398 } 425 399 426 400 DECLARE_WAIT_QUEUE_HEAD(log_wait); ··· 2280 2320 printed_len = vprintk_store(facility, level, dev_info, fmt, args); 2281 2321 2282 2322 /* If called from the scheduler, we can not call up(). */ 2283 - if (!in_sched && allow_direct_printing()) { 2323 + if (!in_sched) { 2284 2324 /* 2285 2325 * The caller may be holding system-critical or 2286 - * timing-sensitive locks. Disable preemption during direct 2326 + * timing-sensitive locks. Disable preemption during 2287 2327 * printing of all remaining records to all consoles so that 2288 2328 * this context can return as soon as possible. Hopefully 2289 2329 * another printk() caller will take over the printing. ··· 2326 2366 2327 2367 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress); 2328 2368 2329 - static void printk_start_kthread(struct console *con); 2330 - 2331 2369 #else /* CONFIG_PRINTK */ 2332 2370 2333 2371 #define CONSOLE_LOG_MAX 0 ··· 2359 2401 } 2360 2402 static bool suppress_message_printing(int level) { return false; } 2361 2403 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; } 2362 - static void printk_start_kthread(struct console *con) { } 2363 - static bool allow_direct_printing(void) { return true; } 2364 2404 2365 2405 #endif /* CONFIG_PRINTK */ 2366 2406 ··· 2559 2603 down_console_sem(); 2560 2604 console_suspended = 0; 2561 2605 console_unlock(); 2562 - 2563 - /* 2564 - * While suspended, new records may have been added to the 2565 - * ringbuffer. Wake up the kthread printers to print them. 2566 - */ 2567 - wake_up_klogd(); 2568 - 2569 2606 pr_flush(1000, true); 2570 2607 } 2571 2608 ··· 2577 2628 /* If trylock fails, someone else is doing the printing */ 2578 2629 if (console_trylock()) 2579 2630 console_unlock(); 2580 - 2581 - /* Wake kthread printers. Some may have become usable. */ 2582 - wake_up_klogd(); 2583 2631 } 2584 2632 return 0; 2585 2633 } ··· 2648 2702 return atomic_read(&panic_cpu) != raw_smp_processor_id(); 2649 2703 } 2650 2704 2651 - static inline bool __console_is_usable(short flags) 2652 - { 2653 - if (!(flags & CON_ENABLED)) 2654 - return false; 2655 - 2656 - /* 2657 - * Console drivers may assume that per-cpu resources have been 2658 - * allocated. So unless they're explicitly marked as being able to 2659 - * cope (CON_ANYTIME) don't call them until this CPU is officially up. 2660 - */ 2661 - if (!cpu_online(raw_smp_processor_id()) && 2662 - !(flags & CON_ANYTIME)) 2663 - return false; 2664 - 2665 - return true; 2666 - } 2667 - 2668 2705 /* 2669 2706 * Check if the given console is currently capable and allowed to print 2670 2707 * records. ··· 2656 2727 */ 2657 2728 static inline bool console_is_usable(struct console *con) 2658 2729 { 2730 + if (!(con->flags & CON_ENABLED)) 2731 + return false; 2732 + 2659 2733 if (!con->write) 2660 2734 return false; 2661 2735 2662 - return __console_is_usable(con->flags); 2736 + /* 2737 + * Console drivers may assume that per-cpu resources have been 2738 + * allocated. So unless they're explicitly marked as being able to 2739 + * cope (CON_ANYTIME) don't call them until this CPU is officially up. 2740 + */ 2741 + if (!cpu_online(raw_smp_processor_id()) && 2742 + !(con->flags & CON_ANYTIME)) 2743 + return false; 2744 + 2745 + return true; 2663 2746 } 2664 2747 2665 2748 static void __console_unlock(void) ··· 2786 2845 * were flushed to all usable consoles. A returned false informs the caller 2787 2846 * that everything was not flushed (either there were no usable consoles or 2788 2847 * another context has taken over printing or it is a panic situation and this 2789 - * is not the panic CPU or direct printing is not preferred). Regardless the 2790 - * reason, the caller should assume it is not useful to immediately try again. 2848 + * is not the panic CPU). Regardless the reason, the caller should assume it 2849 + * is not useful to immediately try again. 2791 2850 * 2792 2851 * Requires the console_lock. 2793 2852 */ ··· 2804 2863 *handover = false; 2805 2864 2806 2865 do { 2807 - /* Let the kthread printers do the work if they can. */ 2808 - if (!allow_direct_printing()) 2809 - return false; 2810 - 2811 2866 any_progress = false; 2812 2867 2813 2868 for_each_console(con) { ··· 3018 3081 console_lock(); 3019 3082 console->flags |= CON_ENABLED; 3020 3083 console_unlock(); 3021 - 3022 - /* Wake the newly enabled kthread printer. */ 3023 - wake_up_klogd(); 3024 - 3025 3084 __pr_flush(console, 1000, true); 3026 3085 } 3027 3086 EXPORT_SYMBOL(console_start); ··· 3218 3285 nr_ext_console_drivers++; 3219 3286 3220 3287 newcon->dropped = 0; 3221 - newcon->thread = NULL; 3222 - 3223 3288 if (newcon->flags & CON_PRINTBUFFER) { 3224 3289 /* Get a consistent copy of @syslog_seq. */ 3225 3290 mutex_lock(&syslog_lock); ··· 3227 3296 /* Begin with next message. */ 3228 3297 newcon->seq = prb_next_seq(prb); 3229 3298 } 3230 - 3231 - if (printk_kthreads_available) 3232 - printk_start_kthread(newcon); 3233 - 3234 3299 console_unlock(); 3235 3300 console_sysfs_notify(); 3236 3301 ··· 3253 3326 3254 3327 int unregister_console(struct console *console) 3255 3328 { 3256 - struct task_struct *thd; 3257 3329 struct console *con; 3258 3330 int res; 3259 3331 ··· 3293 3367 console_drivers->flags |= CON_CONSDEV; 3294 3368 3295 3369 console->flags &= ~CON_ENABLED; 3296 - 3297 - /* 3298 - * console->thread can only be cleared under the console lock. But 3299 - * stopping the thread must be done without the console lock. The 3300 - * task that clears @thread is the task that stops the kthread. 3301 - */ 3302 - thd = console->thread; 3303 - console->thread = NULL; 3304 - 3305 3370 console_unlock(); 3306 - 3307 - if (thd) 3308 - kthread_stop(thd); 3309 - 3310 3371 console_sysfs_notify(); 3311 3372 3312 3373 if (console->exit) ··· 3389 3476 } 3390 3477 late_initcall(printk_late_init); 3391 3478 3392 - static int __init printk_activate_kthreads(void) 3393 - { 3394 - struct console *con; 3395 - 3396 - console_lock(); 3397 - printk_kthreads_available = true; 3398 - for_each_console(con) 3399 - printk_start_kthread(con); 3400 - console_unlock(); 3401 - 3402 - return 0; 3403 - } 3404 - early_initcall(printk_activate_kthreads); 3405 - 3406 3479 #if defined CONFIG_PRINTK 3407 3480 /* If @con is specified, only wait for that console. Otherwise wait for all. */ 3408 3481 static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) ··· 3463 3564 } 3464 3565 EXPORT_SYMBOL(pr_flush); 3465 3566 3466 - static void __printk_fallback_preferred_direct(void) 3467 - { 3468 - printk_prefer_direct_enter(); 3469 - pr_err("falling back to preferred direct printing\n"); 3470 - printk_kthreads_available = false; 3471 - } 3472 - 3473 - /* 3474 - * Enter preferred direct printing, but never exit. Mark console threads as 3475 - * unavailable. The system is then forever in preferred direct printing and 3476 - * any printing threads will exit. 3477 - * 3478 - * Must *not* be called under console_lock. Use 3479 - * __printk_fallback_preferred_direct() if already holding console_lock. 3480 - */ 3481 - static void printk_fallback_preferred_direct(void) 3482 - { 3483 - console_lock(); 3484 - __printk_fallback_preferred_direct(); 3485 - console_unlock(); 3486 - } 3487 - 3488 - static bool printer_should_wake(struct console *con, u64 seq) 3489 - { 3490 - short flags; 3491 - 3492 - if (kthread_should_stop() || !printk_kthreads_available) 3493 - return true; 3494 - 3495 - if (console_suspended) 3496 - return false; 3497 - 3498 - /* 3499 - * This is an unsafe read from con->flags, but a false positive is 3500 - * not a problem. Worst case it would allow the printer to wake up 3501 - * although it is disabled. But the printer will notice that when 3502 - * attempting to print and instead go back to sleep. 3503 - */ 3504 - flags = data_race(READ_ONCE(con->flags)); 3505 - 3506 - if (!__console_is_usable(flags)) 3507 - return false; 3508 - 3509 - return prb_read_valid(prb, seq, NULL); 3510 - } 3511 - 3512 - static int printk_kthread_func(void *data) 3513 - { 3514 - struct console *con = data; 3515 - char *dropped_text = NULL; 3516 - char *ext_text = NULL; 3517 - bool handover; 3518 - u64 seq = 0; 3519 - char *text; 3520 - int error; 3521 - 3522 - text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL); 3523 - if (!text) { 3524 - con_printk(KERN_ERR, con, "failed to allocate text buffer\n"); 3525 - printk_fallback_preferred_direct(); 3526 - goto out; 3527 - } 3528 - 3529 - if (con->flags & CON_EXTENDED) { 3530 - ext_text = kmalloc(CONSOLE_EXT_LOG_MAX, GFP_KERNEL); 3531 - if (!ext_text) { 3532 - con_printk(KERN_ERR, con, "failed to allocate ext_text buffer\n"); 3533 - printk_fallback_preferred_direct(); 3534 - goto out; 3535 - } 3536 - } else { 3537 - dropped_text = kmalloc(DROPPED_TEXT_MAX, GFP_KERNEL); 3538 - if (!dropped_text) { 3539 - con_printk(KERN_ERR, con, "failed to allocate dropped_text buffer\n"); 3540 - printk_fallback_preferred_direct(); 3541 - goto out; 3542 - } 3543 - } 3544 - 3545 - con_printk(KERN_INFO, con, "printing thread started\n"); 3546 - 3547 - for (;;) { 3548 - /* 3549 - * Guarantee this task is visible on the waitqueue before 3550 - * checking the wake condition. 3551 - * 3552 - * The full memory barrier within set_current_state() of 3553 - * prepare_to_wait_event() pairs with the full memory barrier 3554 - * within wq_has_sleeper(). 3555 - * 3556 - * This pairs with __wake_up_klogd:A. 3557 - */ 3558 - error = wait_event_interruptible(log_wait, 3559 - printer_should_wake(con, seq)); /* LMM(printk_kthread_func:A) */ 3560 - 3561 - if (kthread_should_stop() || !printk_kthreads_available) 3562 - break; 3563 - 3564 - if (error) 3565 - continue; 3566 - 3567 - console_lock(); 3568 - 3569 - if (console_suspended) { 3570 - up_console_sem(); 3571 - continue; 3572 - } 3573 - 3574 - if (!console_is_usable(con)) { 3575 - __console_unlock(); 3576 - continue; 3577 - } 3578 - 3579 - /* 3580 - * Even though the printk kthread is always preemptible, it is 3581 - * still not allowed to call cond_resched() from within 3582 - * console drivers. The task may become non-preemptible in the 3583 - * console driver call chain. For example, vt_console_print() 3584 - * takes a spinlock and then can call into fbcon_redraw(), 3585 - * which can conditionally invoke cond_resched(). 3586 - */ 3587 - console_may_schedule = 0; 3588 - console_emit_next_record(con, text, ext_text, dropped_text, &handover); 3589 - if (handover) 3590 - continue; 3591 - 3592 - seq = con->seq; 3593 - 3594 - __console_unlock(); 3595 - } 3596 - 3597 - con_printk(KERN_INFO, con, "printing thread stopped\n"); 3598 - out: 3599 - kfree(dropped_text); 3600 - kfree(ext_text); 3601 - kfree(text); 3602 - 3603 - console_lock(); 3604 - /* 3605 - * If this kthread is being stopped by another task, con->thread will 3606 - * already be NULL. That is fine. The important thing is that it is 3607 - * NULL after the kthread exits. 3608 - */ 3609 - con->thread = NULL; 3610 - console_unlock(); 3611 - 3612 - return 0; 3613 - } 3614 - 3615 - /* Must be called under console_lock. */ 3616 - static void printk_start_kthread(struct console *con) 3617 - { 3618 - /* 3619 - * Do not start a kthread if there is no write() callback. The 3620 - * kthreads assume the write() callback exists. 3621 - */ 3622 - if (!con->write) 3623 - return; 3624 - 3625 - con->thread = kthread_run(printk_kthread_func, con, 3626 - "pr/%s%d", con->name, con->index); 3627 - if (IS_ERR(con->thread)) { 3628 - con->thread = NULL; 3629 - con_printk(KERN_ERR, con, "unable to start printing thread\n"); 3630 - __printk_fallback_preferred_direct(); 3631 - return; 3632 - } 3633 - } 3634 - 3635 3567 /* 3636 3568 * Delayed printk version, for scheduler-internal messages: 3637 3569 */ 3638 - #define PRINTK_PENDING_WAKEUP 0x01 3639 - #define PRINTK_PENDING_DIRECT_OUTPUT 0x02 3570 + #define PRINTK_PENDING_WAKEUP 0x01 3571 + #define PRINTK_PENDING_OUTPUT 0x02 3640 3572 3641 3573 static DEFINE_PER_CPU(int, printk_pending); 3642 3574 ··· 3475 3745 { 3476 3746 int pending = this_cpu_xchg(printk_pending, 0); 3477 3747 3478 - if (pending & PRINTK_PENDING_DIRECT_OUTPUT) { 3479 - printk_prefer_direct_enter(); 3480 - 3748 + if (pending & PRINTK_PENDING_OUTPUT) { 3481 3749 /* If trylock fails, someone else is doing the printing */ 3482 3750 if (console_trylock()) 3483 3751 console_unlock(); 3484 - 3485 - printk_prefer_direct_exit(); 3486 3752 } 3487 3753 3488 3754 if (pending & PRINTK_PENDING_WAKEUP) ··· 3503 3777 * prepare_to_wait_event(), which is called after ___wait_event() adds 3504 3778 * the waiter but before it has checked the wait condition. 3505 3779 * 3506 - * This pairs with devkmsg_read:A, syslog_print:A, and 3507 - * printk_kthread_func:A. 3780 + * This pairs with devkmsg_read:A and syslog_print:A. 3508 3781 */ 3509 3782 if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */ 3510 - (val & PRINTK_PENDING_DIRECT_OUTPUT)) { 3783 + (val & PRINTK_PENDING_OUTPUT)) { 3511 3784 this_cpu_or(printk_pending, val); 3512 3785 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); 3513 3786 } ··· 3524 3799 * New messages may have been added directly to the ringbuffer 3525 3800 * using vprintk_store(), so wake any waiters as well. 3526 3801 */ 3527 - int val = PRINTK_PENDING_WAKEUP; 3528 - 3529 - /* 3530 - * Make sure that some context will print the messages when direct 3531 - * printing is allowed. This happens in situations when the kthreads 3532 - * may not be as reliable or perhaps unusable. 3533 - */ 3534 - if (allow_direct_printing()) 3535 - val |= PRINTK_PENDING_DIRECT_OUTPUT; 3536 - 3537 - __wake_up_klogd(val); 3802 + __wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT); 3538 3803 } 3539 3804 3540 3805 void printk_trigger_flush(void)