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

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

Pull printk updates from Petr Mladek:

- Offload writing printk() messages on consoles to per-console
kthreads.

It prevents soft-lockups when an extensive amount of messages is
printed. It was observed, for example, during boot of large systems
with a lot of peripherals like disks or network interfaces.

It prevents live-lockups that were observed, for example, when
messages about allocation failures were reported and a CPU handled
consoles instead of reclaiming the memory. It was hard to solve even
with rate limiting because it would need to take into account the
amount of messages and the speed of all consoles.

It is a must to have for real time. Otherwise, any printk() might
break latency guarantees.

The per-console kthreads allow to handle each console on its own
speed. Slow consoles do not longer slow down faster ones. And
printk() does not longer unpredictably slows down various code paths.

There are situations when the kthreads are either not available or
not reliable, for example, early boot, suspend, or panic. In these
situations, printk() uses the legacy mode and tries to handle
consoles immediately.

- Add documentation for the printk index.

* tag 'printk-for-5.19' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux:
printk, tracing: fix console tracepoint
printk: remove @console_locked
printk: extend console_lock for per-console locking
printk: add kthread console printers
printk: add functions to prefer direct printing
printk: add pr_flush()
printk: move buffer definitions into console_emit_next_record() caller
printk: refactor and rework printing logic
printk: add con_printk() macro for console details
printk: call boot_delay_msec() in printk_delay()
printk: get caller_id/timestamp after migration disable
printk: wake waiters for safe and NMI contexts
printk: wake up all waiters
printk: add missing memory barrier to wake_up_klogd()
printk: cpu sync always disable interrupts
printk: rename cpulock functions
printk/index: Printk index feature documentation
MAINTAINERS: Add printk indexing maintainers on mention of printk_index

+1194 -341
+1
Documentation/core-api/index.rst
··· 20 20 workqueue 21 21 printk-basics 22 22 printk-formats 23 + printk-index 23 24 symbol-namespaces 24 25 25 26 Data structures and low-level utilities
+137
Documentation/core-api/printk-index.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0 2 + 3 + ============ 4 + Printk Index 5 + ============ 6 + 7 + There are many ways how to monitor the state of the system. One important 8 + source of information is the system log. It provides a lot of information, 9 + including more or less important warnings and error messages. 10 + 11 + There are monitoring tools that filter and take action based on messages 12 + logged. 13 + 14 + The kernel messages are evolving together with the code. As a result, 15 + particular kernel messages are not KABI and never will be! 16 + 17 + It is a huge challenge for maintaining the system log monitors. It requires 18 + knowing what messages were updated in a particular kernel version and why. 19 + Finding these changes in the sources would require non-trivial parsers. 20 + Also it would require matching the sources with the binary kernel which 21 + is not always trivial. Various changes might be backported. Various kernel 22 + versions might be used on different monitored systems. 23 + 24 + This is where the printk index feature might become useful. It provides 25 + a dump of printk formats used all over the source code used for the kernel 26 + and modules on the running system. It is accessible at runtime via debugfs. 27 + 28 + The printk index helps to find changes in the message formats. Also it helps 29 + to track the strings back to the kernel sources and the related commit. 30 + 31 + 32 + User Interface 33 + ============== 34 + 35 + The index of printk formats are split in into separate files. The files are 36 + named according to the binaries where the printk formats are built-in. There 37 + is always "vmlinux" and optionally also modules, for example:: 38 + 39 + /sys/kernel/debug/printk/index/vmlinux 40 + /sys/kernel/debug/printk/index/ext4 41 + /sys/kernel/debug/printk/index/scsi_mod 42 + 43 + Note that only loaded modules are shown. Also printk formats from a module 44 + might appear in "vmlinux" when the module is built-in. 45 + 46 + The content is inspired by the dynamic debug interface and looks like:: 47 + 48 + $> head -1 /sys/kernel/debug/printk/index/vmlinux; shuf -n 5 vmlinux 49 + # <level[,flags]> filename:line function "format" 50 + <5> block/blk-settings.c:661 disk_stack_limits "%s: Warning: Device %s is misaligned\n" 51 + <4> kernel/trace/trace.c:8296 trace_create_file "Could not create tracefs '%s' entry\n" 52 + <6> arch/x86/kernel/hpet.c:144 _hpet_print_config "hpet: %s(%d):\n" 53 + <6> init/do_mounts.c:605 prepare_namespace "Waiting for root device %s...\n" 54 + <6> drivers/acpi/osl.c:1410 acpi_no_auto_serialize_setup "ACPI: auto-serialization disabled\n" 55 + 56 + , where the meaning is: 57 + 58 + - :level: log level value: 0-7 for particular severity, -1 as default, 59 + 'c' as continuous line without an explicit log level 60 + - :flags: optional flags: currently only 'c' for KERN_CONT 61 + - :filename\:line: source filename and line number of the related 62 + printk() call. Note that there are many wrappers, for example, 63 + pr_warn(), pr_warn_once(), dev_warn(). 64 + - :function: function name where the printk() call is used. 65 + - :format: format string 66 + 67 + The extra information makes it a bit harder to find differences 68 + between various kernels. Especially the line number might change 69 + very often. On the other hand, it helps a lot to confirm that 70 + it is the same string or find the commit that is responsible 71 + for eventual changes. 72 + 73 + 74 + printk() Is Not a Stable KABI 75 + ============================= 76 + 77 + Several developers are afraid that exporting all these implementation 78 + details into the user space will transform particular printk() calls 79 + into KABI. 80 + 81 + But it is exactly the opposite. printk() calls must _not_ be KABI. 82 + And the printk index helps user space tools to deal with this. 83 + 84 + 85 + Subsystem specific printk wrappers 86 + ================================== 87 + 88 + The printk index is generated using extra metadata that are stored in 89 + a dedicated .elf section ".printk_index". It is achieved using macro 90 + wrappers doing __printk_index_emit() together with the real printk() 91 + call. The same technique is used also for the metadata used by 92 + the dynamic debug feature. 93 + 94 + The metadata are stored for a particular message only when it is printed 95 + using these special wrappers. It is implemented for the commonly 96 + used printk() calls, including, for example, pr_warn(), or pr_once(). 97 + 98 + Additional changes are necessary for various subsystem specific wrappers 99 + that call the original printk() via a common helper function. These needs 100 + their own wrappers adding __printk_index_emit(). 101 + 102 + Only few subsystem specific wrappers have been updated so far, 103 + for example, dev_printk(). As a result, the printk formats from 104 + some subsystes can be missing in the printk index. 105 + 106 + 107 + Subsystem specific prefix 108 + ========================= 109 + 110 + The macro pr_fmt() macro allows to define a prefix that is printed 111 + before the string generated by the related printk() calls. 112 + 113 + Subsystem specific wrappers usually add even more complicated 114 + prefixes. 115 + 116 + These prefixes can be stored into the printk index metadata 117 + by an optional parameter of __printk_index_emit(). The debugfs 118 + interface might then show the printk formats including these prefixes. 119 + For example, drivers/acpi/osl.c contains:: 120 + 121 + #define pr_fmt(fmt) "ACPI: OSL: " fmt 122 + 123 + static int __init acpi_no_auto_serialize_setup(char *str) 124 + { 125 + acpi_gbl_auto_serialize_methods = FALSE; 126 + pr_info("Auto-serialization disabled\n"); 127 + 128 + return 1; 129 + } 130 + 131 + This results in the following printk index entry:: 132 + 133 + <6> drivers/acpi/osl.c:1410 acpi_no_auto_serialize_setup "ACPI: auto-serialization disabled\n" 134 + 135 + It helps matching messages from the real log with printk index. 136 + Then the source file name, line number, and function name can 137 + be used to match the string with the source code.
+2
MAINTAINERS
··· 15923 15923 PRINTK INDEXING 15924 15924 R: Chris Down <chris@chrisdown.name> 15925 15925 S: Maintained 15926 + F: Documentation/core-api/printk-index.rst 15926 15927 F: kernel/printk/index.c 15928 + K: printk_index 15927 15929 15928 15930 PROC FILESYSTEM 15929 15931 L: linux-kernel@vger.kernel.org
+2
drivers/tty/sysrq.c
··· 578 578 579 579 rcu_sysrq_start(); 580 580 rcu_read_lock(); 581 + printk_prefer_direct_enter(); 581 582 /* 582 583 * Raise the apparent loglevel to maximum so that the sysrq header 583 584 * is shown to provide the user with positive feedback. We do not ··· 620 619 pr_cont("\n"); 621 620 console_loglevel = orig_log_level; 622 621 } 622 + printk_prefer_direct_exit(); 623 623 rcu_read_unlock(); 624 624 rcu_sysrq_end(); 625 625
+19
include/linux/console.h
··· 16 16 17 17 #include <linux/atomic.h> 18 18 #include <linux/types.h> 19 + #include <linux/mutex.h> 19 20 20 21 struct vc_data; 21 22 struct console_font_op; ··· 152 151 int cflag; 153 152 uint ispeed; 154 153 uint ospeed; 154 + u64 seq; 155 + 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 + 155 172 void *data; 156 173 struct console *next; 157 174 };
+65 -35
include/linux/printk.h
··· 170 170 #define printk_deferred_enter __printk_safe_enter 171 171 #define printk_deferred_exit __printk_safe_exit 172 172 173 + extern void printk_prefer_direct_enter(void); 174 + extern void printk_prefer_direct_exit(void); 175 + 176 + extern bool pr_flush(int timeout_ms, bool reset_on_progress); 177 + 173 178 /* 174 179 * Please don't use printk_ratelimit(), because it shares ratelimiting state 175 180 * with all other unrelated printk_ratelimit() callsites. Instead use ··· 223 218 224 219 static inline void printk_deferred_exit(void) 225 220 { 221 + } 222 + 223 + static inline void printk_prefer_direct_enter(void) 224 + { 225 + } 226 + 227 + static inline void printk_prefer_direct_exit(void) 228 + { 229 + } 230 + 231 + static inline bool pr_flush(int timeout_ms, bool reset_on_progress) 232 + { 233 + return true; 226 234 } 227 235 228 236 static inline int printk_ratelimit(void) ··· 295 277 #endif 296 278 297 279 #ifdef CONFIG_SMP 298 - extern int __printk_cpu_trylock(void); 299 - extern void __printk_wait_on_cpu_lock(void); 300 - extern void __printk_cpu_unlock(void); 301 - 302 - /** 303 - * printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning 304 - * lock and disable interrupts. 305 - * @flags: Stack-allocated storage for saving local interrupt state, 306 - * to be passed to printk_cpu_unlock_irqrestore(). 307 - * 308 - * If the lock is owned by another CPU, spin until it becomes available. 309 - * Interrupts are restored while spinning. 310 - */ 311 - #define printk_cpu_lock_irqsave(flags) \ 312 - for (;;) { \ 313 - local_irq_save(flags); \ 314 - if (__printk_cpu_trylock()) \ 315 - break; \ 316 - local_irq_restore(flags); \ 317 - __printk_wait_on_cpu_lock(); \ 318 - } 319 - 320 - /** 321 - * printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinning 322 - * lock and restore interrupts. 323 - * @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave(). 324 - */ 325 - #define printk_cpu_unlock_irqrestore(flags) \ 326 - do { \ 327 - __printk_cpu_unlock(); \ 328 - local_irq_restore(flags); \ 329 - } while (0) \ 280 + extern int __printk_cpu_sync_try_get(void); 281 + extern void __printk_cpu_sync_wait(void); 282 + extern void __printk_cpu_sync_put(void); 330 283 331 284 #else 332 285 333 - #define printk_cpu_lock_irqsave(flags) ((void)flags) 334 - #define printk_cpu_unlock_irqrestore(flags) ((void)flags) 335 - 286 + #define __printk_cpu_sync_try_get() true 287 + #define __printk_cpu_sync_wait() 288 + #define __printk_cpu_sync_put() 336 289 #endif /* CONFIG_SMP */ 290 + 291 + /** 292 + * printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the printk 293 + * cpu-reentrant spinning lock. 294 + * @flags: Stack-allocated storage for saving local interrupt state, 295 + * to be passed to printk_cpu_sync_put_irqrestore(). 296 + * 297 + * If the lock is owned by another CPU, spin until it becomes available. 298 + * Interrupts are restored while spinning. 299 + * 300 + * CAUTION: This function must be used carefully. It does not behave like a 301 + * typical lock. Here are important things to watch out for... 302 + * 303 + * * This function is reentrant on the same CPU. Therefore the calling 304 + * code must not assume exclusive access to data if code accessing the 305 + * data can run reentrant or within NMI context on the same CPU. 306 + * 307 + * * If there exists usage of this function from NMI context, it becomes 308 + * unsafe to perform any type of locking or spinning to wait for other 309 + * CPUs after calling this function from any context. This includes 310 + * using spinlocks or any other busy-waiting synchronization methods. 311 + */ 312 + #define printk_cpu_sync_get_irqsave(flags) \ 313 + for (;;) { \ 314 + local_irq_save(flags); \ 315 + if (__printk_cpu_sync_try_get()) \ 316 + break; \ 317 + local_irq_restore(flags); \ 318 + __printk_cpu_sync_wait(); \ 319 + } 320 + 321 + /** 322 + * printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spinning 323 + * lock and restore interrupts. 324 + * @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsave(). 325 + */ 326 + #define printk_cpu_sync_put_irqrestore(flags) \ 327 + do { \ 328 + __printk_cpu_sync_put(); \ 329 + local_irq_restore(flags); \ 330 + } while (0) 337 331 338 332 extern int kptr_restrict; 339 333
+10 -1
kernel/hung_task.c
··· 127 127 * complain: 128 128 */ 129 129 if (sysctl_hung_task_warnings) { 130 + printk_prefer_direct_enter(); 131 + 130 132 if (sysctl_hung_task_warnings > 0) 131 133 sysctl_hung_task_warnings--; 132 134 pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n", ··· 144 142 145 143 if (sysctl_hung_task_all_cpu_backtrace) 146 144 hung_task_show_all_bt = true; 145 + 146 + printk_prefer_direct_exit(); 147 147 } 148 148 149 149 touch_nmi_watchdog(); ··· 208 204 } 209 205 unlock: 210 206 rcu_read_unlock(); 211 - if (hung_task_show_lock) 207 + if (hung_task_show_lock) { 208 + printk_prefer_direct_enter(); 212 209 debug_show_all_locks(); 210 + printk_prefer_direct_exit(); 211 + } 213 212 214 213 if (hung_task_show_all_bt) { 215 214 hung_task_show_all_bt = false; 215 + printk_prefer_direct_enter(); 216 216 trigger_all_cpu_backtrace(); 217 + printk_prefer_direct_exit(); 217 218 } 218 219 219 220 if (hung_task_call_panic)
+4
kernel/panic.c
··· 579 579 { 580 580 disable_trace_on_warning(); 581 581 582 + printk_prefer_direct_enter(); 583 + 582 584 if (file) 583 585 pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", 584 586 raw_smp_processor_id(), current->pid, file, line, ··· 610 608 611 609 /* Just a warning, don't kill lockdep. */ 612 610 add_taint(taint, LOCKDEP_STILL_OK); 611 + 612 + printk_prefer_direct_exit(); 613 613 } 614 614 615 615 #ifndef __WARN_FLAGS
+927 -300
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 + /* 227 254 * Helper macros to handle lockdep when locking/unlocking console_sem. We use 228 255 * macros instead of functions so that _RET_IP_ contains useful information. 229 256 */ ··· 298 271 } 299 272 300 273 /* 301 - * This is used for debugging the mess that is the VT code by 302 - * keeping track if we have the console semaphore held. It's 303 - * definitely not the perfect debug tool (we don't know if _WE_ 304 - * hold it and are racing, but it helps tracking those weird code 305 - * paths in the console code where we end up in places I want 306 - * locked without the console semaphore held). 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. 307 277 */ 308 - static int console_locked, console_suspended; 278 + static bool console_kthreads_blocked; 309 279 310 280 /* 311 - * If exclusive_console is non-NULL then only this console is to be printed to. 281 + * Block all kthread printers from a schedulable context. 282 + * 283 + * Requires holding @console_sem. 312 284 */ 313 - static struct console *exclusive_console; 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; 314 317 315 318 /* 316 319 * Array of consoles built from command line options (console=) ··· 423 366 /* syslog_lock protects syslog_* variables and write access to clear_seq. */ 424 367 static DEFINE_MUTEX(syslog_lock); 425 368 369 + /* 370 + * A flag to signify if printk_activate_kthreads() has already started the 371 + * kthread printers. If true, any later registered consoles must start their 372 + * own kthread directly. The flag is write protected by the console_lock. 373 + */ 374 + static bool printk_kthreads_available; 375 + 426 376 #ifdef CONFIG_PRINTK 377 + static atomic_t printk_prefer_direct = ATOMIC_INIT(0); 378 + 379 + /** 380 + * printk_prefer_direct_enter - cause printk() calls to attempt direct 381 + * printing to all enabled consoles 382 + * 383 + * Since it is not possible to call into the console printing code from any 384 + * context, there is no guarantee that direct printing will occur. 385 + * 386 + * This globally effects all printk() callers. 387 + * 388 + * Context: Any context. 389 + */ 390 + void printk_prefer_direct_enter(void) 391 + { 392 + atomic_inc(&printk_prefer_direct); 393 + } 394 + 395 + /** 396 + * printk_prefer_direct_exit - restore printk() behavior 397 + * 398 + * Context: Any context. 399 + */ 400 + void printk_prefer_direct_exit(void) 401 + { 402 + WARN_ON(atomic_dec_if_positive(&printk_prefer_direct) < 0); 403 + } 404 + 405 + /* 406 + * Calling printk() always wakes kthread printers so that they can 407 + * flush the new message to their respective consoles. Also, if direct 408 + * printing is allowed, printk() tries to flush the messages directly. 409 + * 410 + * Direct printing is allowed in situations when the kthreads 411 + * are not available or the system is in a problematic state. 412 + * 413 + * See the implementation about possible races. 414 + */ 415 + static inline bool allow_direct_printing(void) 416 + { 417 + /* 418 + * Checking kthread availability is a possible race because the 419 + * kthread printers can become permanently disabled during runtime. 420 + * However, doing that requires holding the console_lock, so any 421 + * pending messages will be direct printed by console_unlock(). 422 + */ 423 + if (!printk_kthreads_available) 424 + return true; 425 + 426 + /* 427 + * Prefer direct printing when the system is in a problematic state. 428 + * The context that sets this state will always see the updated value. 429 + * The other contexts do not care. Anyway, direct printing is just a 430 + * best effort. The direct output is only possible when console_lock 431 + * is not already taken and no kthread printers are actively printing. 432 + */ 433 + return (system_state > SYSTEM_RUNNING || 434 + oops_in_progress || 435 + atomic_read(&printk_prefer_direct)); 436 + } 437 + 427 438 DECLARE_WAIT_QUEUE_HEAD(log_wait); 428 439 /* All 3 protected by @syslog_lock. */ 429 440 /* the next printk record to read by syslog(READ) or /proc/kmsg */ 430 441 static u64 syslog_seq; 431 442 static size_t syslog_partial; 432 443 static bool syslog_time; 433 - 434 - /* All 3 protected by @console_sem. */ 435 - /* the next printk record to write to the console */ 436 - static u64 console_seq; 437 - static u64 exclusive_console_stop_seq; 438 - static unsigned long console_dropped; 439 444 440 445 struct latched_seq { 441 446 seqcount_latch_t latch; ··· 523 404 524 405 /* the maximum size of a formatted record (i.e. with prefix added per line) */ 525 406 #define CONSOLE_LOG_MAX 1024 407 + 408 + /* the maximum size for a dropped text message */ 409 + #define DROPPED_TEXT_MAX 64 526 410 527 411 /* the maximum size allowed to be reserved for a record */ 528 412 #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) ··· 868 746 goto out; 869 747 } 870 748 749 + /* 750 + * Guarantee this task is visible on the waitqueue before 751 + * checking the wake condition. 752 + * 753 + * The full memory barrier within set_current_state() of 754 + * prepare_to_wait_event() pairs with the full memory barrier 755 + * within wq_has_sleeper(). 756 + * 757 + * This pairs with __wake_up_klogd:A. 758 + */ 871 759 ret = wait_event_interruptible(log_wait, 872 - prb_read_valid(prb, atomic64_read(&user->seq), r)); 760 + prb_read_valid(prb, 761 + atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */ 873 762 if (ret) 874 763 goto out; 875 764 } ··· 1646 1513 seq = syslog_seq; 1647 1514 1648 1515 mutex_unlock(&syslog_lock); 1649 - len = wait_event_interruptible(log_wait, prb_read_valid(prb, seq, NULL)); 1516 + /* 1517 + * Guarantee this task is visible on the waitqueue before 1518 + * checking the wake condition. 1519 + * 1520 + * The full memory barrier within set_current_state() of 1521 + * prepare_to_wait_event() pairs with the full memory barrier 1522 + * within wq_has_sleeper(). 1523 + * 1524 + * This pairs with __wake_up_klogd:A. 1525 + */ 1526 + len = wait_event_interruptible(log_wait, 1527 + prb_read_valid(prb, seq, NULL)); /* LMM(syslog_print:A) */ 1650 1528 mutex_lock(&syslog_lock); 1651 1529 1652 1530 if (len) ··· 2055 1911 } 2056 1912 2057 1913 /* 2058 - * Call the console drivers, asking them to write out 2059 - * log_buf[start] to log_buf[end - 1]. 2060 - * The console_lock must be held. 1914 + * Call the specified console driver, asking it to write out the specified 1915 + * text and length. If @dropped_text is non-NULL and any records have been 1916 + * dropped, a dropped message will be written out first. 2061 1917 */ 2062 - static void call_console_drivers(const char *ext_text, size_t ext_len, 2063 - const char *text, size_t len) 1918 + static void call_console_driver(struct console *con, const char *text, size_t len, 1919 + char *dropped_text) 2064 1920 { 2065 - static char dropped_text[64]; 2066 - size_t dropped_len = 0; 2067 - struct console *con; 1921 + size_t dropped_len; 2068 1922 2069 - trace_console_rcuidle(text, len); 2070 - 2071 - if (!console_drivers) 2072 - return; 2073 - 2074 - if (console_dropped) { 2075 - dropped_len = snprintf(dropped_text, sizeof(dropped_text), 1923 + if (con->dropped && dropped_text) { 1924 + dropped_len = snprintf(dropped_text, DROPPED_TEXT_MAX, 2076 1925 "** %lu printk messages dropped **\n", 2077 - console_dropped); 2078 - console_dropped = 0; 1926 + con->dropped); 1927 + con->dropped = 0; 1928 + con->write(con, dropped_text, dropped_len); 2079 1929 } 2080 1930 2081 - for_each_console(con) { 2082 - if (exclusive_console && con != exclusive_console) 2083 - continue; 2084 - if (!(con->flags & CON_ENABLED)) 2085 - continue; 2086 - if (!con->write) 2087 - continue; 2088 - if (!cpu_online(smp_processor_id()) && 2089 - !(con->flags & CON_ANYTIME)) 2090 - continue; 2091 - if (con->flags & CON_EXTENDED) 2092 - con->write(con, ext_text, ext_len); 2093 - else { 2094 - if (dropped_len) 2095 - con->write(con, dropped_text, dropped_len); 2096 - con->write(con, text, len); 2097 - } 2098 - } 1931 + con->write(con, text, len); 2099 1932 } 2100 1933 2101 1934 /* ··· 2147 2026 2148 2027 int printk_delay_msec __read_mostly; 2149 2028 2150 - static inline void printk_delay(void) 2029 + static inline void printk_delay(int level) 2151 2030 { 2031 + boot_delay_msec(level); 2032 + 2152 2033 if (unlikely(printk_delay_msec)) { 2153 2034 int m = printk_delay_msec; 2154 2035 ··· 2164 2041 static inline u32 printk_caller_id(void) 2165 2042 { 2166 2043 return in_task() ? task_pid_nr(current) : 2167 - 0x80000000 + raw_smp_processor_id(); 2044 + 0x80000000 + smp_processor_id(); 2168 2045 } 2169 2046 2170 2047 /** ··· 2238 2115 } 2239 2116 } 2240 2117 2118 + trace_console_rcuidle(text, text_len); 2119 + 2241 2120 return text_len; 2242 2121 } 2243 2122 ··· 2248 2123 const struct dev_printk_info *dev_info, 2249 2124 const char *fmt, va_list args) 2250 2125 { 2251 - const u32 caller_id = printk_caller_id(); 2252 2126 struct prb_reserved_entry e; 2253 2127 enum printk_info_flags flags = 0; 2254 2128 struct printk_record r; ··· 2257 2133 u8 *recursion_ptr; 2258 2134 u16 reserve_size; 2259 2135 va_list args2; 2136 + u32 caller_id; 2260 2137 u16 text_len; 2261 2138 int ret = 0; 2262 2139 u64 ts_nsec; 2140 + 2141 + if (!printk_enter_irqsave(recursion_ptr, irqflags)) 2142 + return 0; 2263 2143 2264 2144 /* 2265 2145 * Since the duration of printk() can vary depending on the message ··· 2273 2145 */ 2274 2146 ts_nsec = local_clock(); 2275 2147 2276 - if (!printk_enter_irqsave(recursion_ptr, irqflags)) 2277 - return 0; 2148 + caller_id = printk_caller_id(); 2278 2149 2279 2150 /* 2280 2151 * The sprintf needs to come first since the syslog prefix might be ··· 2377 2250 in_sched = true; 2378 2251 } 2379 2252 2380 - boot_delay_msec(level); 2381 - printk_delay(); 2253 + printk_delay(level); 2382 2254 2383 2255 printed_len = vprintk_store(facility, level, dev_info, fmt, args); 2384 2256 2385 2257 /* If called from the scheduler, we can not call up(). */ 2386 - if (!in_sched) { 2258 + if (!in_sched && allow_direct_printing()) { 2387 2259 /* 2388 - * Disable preemption to avoid being preempted while holding 2389 - * console_sem which would prevent anyone from printing to 2390 - * console 2260 + * The caller may be holding system-critical or 2261 + * timing-sensitive locks. Disable preemption during direct 2262 + * printing of all remaining records to all consoles so that 2263 + * this context can return as soon as possible. Hopefully 2264 + * another printk() caller will take over the printing. 2391 2265 */ 2392 2266 preempt_disable(); 2393 2267 /* 2394 2268 * Try to acquire and then immediately release the console 2395 - * semaphore. The release will print out buffers and wake up 2396 - * /dev/kmsg and syslog() users. 2269 + * semaphore. The release will print out buffers. With the 2270 + * spinning variant, this context tries to take over the 2271 + * printing from another printing context. 2397 2272 */ 2398 2273 if (console_trylock_spinning()) 2399 2274 console_unlock(); ··· 2426 2297 } 2427 2298 EXPORT_SYMBOL(_printk); 2428 2299 2300 + static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress); 2301 + 2302 + static void printk_start_kthread(struct console *con); 2303 + 2429 2304 #else /* CONFIG_PRINTK */ 2430 2305 2431 2306 #define CONSOLE_LOG_MAX 0 2307 + #define DROPPED_TEXT_MAX 0 2432 2308 #define printk_time false 2433 2309 2434 2310 #define prb_read_valid(rb, seq, r) false 2435 2311 #define prb_first_valid_seq(rb) 0 2312 + #define prb_next_seq(rb) 0 2436 2313 2437 2314 static u64 syslog_seq; 2438 - static u64 console_seq; 2439 - static u64 exclusive_console_stop_seq; 2440 - static unsigned long console_dropped; 2441 2315 2442 2316 static size_t record_print_text(const struct printk_record *r, 2443 2317 bool syslog, bool time) ··· 2457 2325 struct dev_printk_info *dev_info) { return 0; } 2458 2326 static void console_lock_spinning_enable(void) { } 2459 2327 static int console_lock_spinning_disable_and_check(void) { return 0; } 2460 - static void call_console_drivers(const char *ext_text, size_t ext_len, 2461 - const char *text, size_t len) {} 2328 + static void call_console_driver(struct console *con, const char *text, size_t len, 2329 + char *dropped_text) 2330 + { 2331 + } 2462 2332 static bool suppress_message_printing(int level) { return false; } 2333 + 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; } 2463 2336 2464 2337 #endif /* CONFIG_PRINTK */ 2465 2338 ··· 2652 2515 if (!console_suspend_enabled) 2653 2516 return; 2654 2517 pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); 2518 + pr_flush(1000, true); 2655 2519 console_lock(); 2656 2520 console_suspended = 1; 2657 2521 up_console_sem(); ··· 2665 2527 down_console_sem(); 2666 2528 console_suspended = 0; 2667 2529 console_unlock(); 2530 + pr_flush(1000, true); 2668 2531 } 2669 2532 2670 2533 /** ··· 2683 2544 /* If trylock fails, someone else is doing the printing */ 2684 2545 if (console_trylock()) 2685 2546 console_unlock(); 2547 + else { 2548 + /* 2549 + * If a new CPU comes online, the conditions for 2550 + * printer_should_wake() may have changed for some 2551 + * kthread printer with !CON_ANYTIME. 2552 + */ 2553 + wake_up_klogd(); 2554 + } 2686 2555 } 2687 2556 return 0; 2688 2557 } ··· 2710 2563 down_console_sem(); 2711 2564 if (console_suspended) 2712 2565 return; 2713 - console_locked = 1; 2566 + console_kthreads_block(); 2714 2567 console_may_schedule = 1; 2715 2568 } 2716 2569 EXPORT_SYMBOL(console_lock); ··· 2731 2584 up_console_sem(); 2732 2585 return 0; 2733 2586 } 2734 - console_locked = 1; 2587 + if (!console_kthreads_atomic_tryblock()) { 2588 + up_console_sem(); 2589 + return 0; 2590 + } 2735 2591 console_may_schedule = 0; 2736 2592 return 1; 2737 2593 } 2738 2594 EXPORT_SYMBOL(console_trylock); 2739 2595 2596 + /* 2597 + * This is used to help to make sure that certain paths within the VT code are 2598 + * running with the console lock held. It is definitely not the perfect debug 2599 + * tool (it is not known if the VT code is the task holding the console lock), 2600 + * but it helps tracking those weird code paths in the console code such as 2601 + * when the console is suspended: where the console is not locked but no 2602 + * console printing may occur. 2603 + * 2604 + * Note: This returns true when the console is suspended but is not locked. 2605 + * This is intentional because the VT code must consider that situation 2606 + * the same as if the console was locked. 2607 + */ 2740 2608 int is_console_locked(void) 2741 2609 { 2742 - return console_locked; 2610 + return (console_kthreads_blocked || atomic_read(&console_kthreads_active)); 2743 2611 } 2744 2612 EXPORT_SYMBOL(is_console_locked); 2745 - 2746 - /* 2747 - * Check if we have any console that is capable of printing while cpu is 2748 - * booting or shutting down. Requires console_sem. 2749 - */ 2750 - static int have_callable_console(void) 2751 - { 2752 - struct console *con; 2753 - 2754 - for_each_console(con) 2755 - if ((con->flags & CON_ENABLED) && 2756 - (con->flags & CON_ANYTIME)) 2757 - return 1; 2758 - 2759 - return 0; 2760 - } 2761 2613 2762 2614 /* 2763 2615 * Return true when this CPU should unlock console_sem without pushing all ··· 2777 2631 return atomic_read(&panic_cpu) != raw_smp_processor_id(); 2778 2632 } 2779 2633 2780 - /* 2781 - * Can we actually use the console at this time on this cpu? 2782 - * 2783 - * Console drivers may assume that per-cpu resources have been allocated. So 2784 - * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't 2785 - * call them until this CPU is officially up. 2786 - */ 2787 - static inline int can_use_console(void) 2634 + static inline bool __console_is_usable(short flags) 2788 2635 { 2789 - return cpu_online(raw_smp_processor_id()) || have_callable_console(); 2636 + if (!(flags & CON_ENABLED)) 2637 + return false; 2638 + 2639 + /* 2640 + * Console drivers may assume that per-cpu resources have been 2641 + * allocated. So unless they're explicitly marked as being able to 2642 + * cope (CON_ANYTIME) don't call them until this CPU is officially up. 2643 + */ 2644 + if (!cpu_online(raw_smp_processor_id()) && 2645 + !(flags & CON_ANYTIME)) 2646 + return false; 2647 + 2648 + return true; 2790 2649 } 2791 2650 2792 - /** 2793 - * console_unlock - unlock the console system 2651 + /* 2652 + * Check if the given console is currently capable and allowed to print 2653 + * records. 2794 2654 * 2795 - * Releases the console_lock which the caller holds on the console system 2796 - * and the console driver list. 2797 - * 2798 - * While the console_lock was held, console output may have been buffered 2799 - * by printk(). If this is the case, console_unlock(); emits 2800 - * the output prior to releasing the lock. 2801 - * 2802 - * If there is output waiting, we wake /dev/kmsg and syslog() users. 2803 - * 2804 - * console_unlock(); may be called from any context. 2655 + * Requires holding the console_lock. 2805 2656 */ 2806 - void console_unlock(void) 2657 + static inline bool console_is_usable(struct console *con) 2807 2658 { 2808 - static char ext_text[CONSOLE_EXT_LOG_MAX]; 2809 - static char text[CONSOLE_LOG_MAX]; 2810 - static int panic_console_dropped; 2811 - unsigned long flags; 2812 - bool do_cond_resched, retry; 2659 + if (!con->write) 2660 + return false; 2661 + 2662 + return __console_is_usable(con->flags); 2663 + } 2664 + 2665 + static void __console_unlock(void) 2666 + { 2667 + /* 2668 + * Depending on whether console_lock() or console_trylock() was used, 2669 + * appropriately allow the kthread printers to continue. 2670 + */ 2671 + if (console_kthreads_blocked) 2672 + console_kthreads_unblock(); 2673 + else 2674 + console_kthreads_atomic_unblock(); 2675 + 2676 + /* 2677 + * New records may have arrived while the console was locked. 2678 + * Wake the kthread printers to print them. 2679 + */ 2680 + wake_up_klogd(); 2681 + 2682 + up_console_sem(); 2683 + } 2684 + 2685 + /* 2686 + * Print one record for the given console. The record printed is whatever 2687 + * record is the next available record for the given console. 2688 + * 2689 + * @text is a buffer of size CONSOLE_LOG_MAX. 2690 + * 2691 + * If extended messages should be printed, @ext_text is a buffer of size 2692 + * CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL. 2693 + * 2694 + * If dropped messages should be printed, @dropped_text is a buffer of size 2695 + * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL. 2696 + * 2697 + * @handover will be set to true if a printk waiter has taken over the 2698 + * console_lock, in which case the caller is no longer holding the 2699 + * console_lock. Otherwise it is set to false. A NULL pointer may be provided 2700 + * to disable allowing the console_lock to be taken over by a printk waiter. 2701 + * 2702 + * Returns false if the given console has no next record to print, otherwise 2703 + * true. 2704 + * 2705 + * Requires the console_lock if @handover is non-NULL. 2706 + * Requires con->lock otherwise. 2707 + */ 2708 + static bool __console_emit_next_record(struct console *con, char *text, char *ext_text, 2709 + char *dropped_text, bool *handover) 2710 + { 2711 + static atomic_t panic_console_dropped = ATOMIC_INIT(0); 2813 2712 struct printk_info info; 2814 2713 struct printk_record r; 2815 - u64 __maybe_unused next_seq; 2714 + unsigned long flags; 2715 + char *write_text; 2716 + size_t len; 2816 2717 2817 - if (console_suspended) { 2818 - up_console_sem(); 2819 - return; 2718 + prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); 2719 + 2720 + if (handover) 2721 + *handover = false; 2722 + 2723 + if (!prb_read_valid(prb, con->seq, &r)) 2724 + return false; 2725 + 2726 + if (con->seq != r.info->seq) { 2727 + con->dropped += r.info->seq - con->seq; 2728 + con->seq = r.info->seq; 2729 + if (panic_in_progress() && 2730 + atomic_fetch_inc_relaxed(&panic_console_dropped) > 10) { 2731 + suppress_panic_printk = 1; 2732 + pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); 2733 + } 2820 2734 } 2821 2735 2822 - prb_rec_init_rd(&r, &info, text, sizeof(text)); 2823 - 2824 - /* 2825 - * Console drivers are called with interrupts disabled, so 2826 - * @console_may_schedule should be cleared before; however, we may 2827 - * end up dumping a lot of lines, for example, if called from 2828 - * console registration path, and should invoke cond_resched() 2829 - * between lines if allowable. Not doing so can cause a very long 2830 - * scheduling stall on a slow console leading to RCU stall and 2831 - * softlockup warnings which exacerbate the issue with more 2832 - * messages practically incapacitating the system. 2833 - * 2834 - * console_trylock() is not able to detect the preemptive 2835 - * context reliably. Therefore the value must be stored before 2836 - * and cleared after the "again" goto label. 2837 - */ 2838 - do_cond_resched = console_may_schedule; 2839 - again: 2840 - console_may_schedule = 0; 2841 - 2842 - /* 2843 - * We released the console_sem lock, so we need to recheck if 2844 - * cpu is online and (if not) is there at least one CON_ANYTIME 2845 - * console. 2846 - */ 2847 - if (!can_use_console()) { 2848 - console_locked = 0; 2849 - up_console_sem(); 2850 - return; 2736 + /* Skip record that has level above the console loglevel. */ 2737 + if (suppress_message_printing(r.info->level)) { 2738 + con->seq++; 2739 + goto skip; 2851 2740 } 2852 2741 2853 - for (;;) { 2854 - size_t ext_len = 0; 2855 - int handover; 2856 - size_t len; 2742 + if (ext_text) { 2743 + write_text = ext_text; 2744 + len = info_print_ext_header(ext_text, CONSOLE_EXT_LOG_MAX, r.info); 2745 + len += msg_print_ext_body(ext_text + len, CONSOLE_EXT_LOG_MAX - len, 2746 + &r.text_buf[0], r.info->text_len, &r.info->dev_info); 2747 + } else { 2748 + write_text = text; 2749 + len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time); 2750 + } 2857 2751 2858 - skip: 2859 - if (!prb_read_valid(prb, console_seq, &r)) 2860 - break; 2861 - 2862 - if (console_seq != r.info->seq) { 2863 - console_dropped += r.info->seq - console_seq; 2864 - console_seq = r.info->seq; 2865 - if (panic_in_progress() && panic_console_dropped++ > 10) { 2866 - suppress_panic_printk = 1; 2867 - pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); 2868 - } 2869 - } 2870 - 2871 - if (suppress_message_printing(r.info->level)) { 2872 - /* 2873 - * Skip record we have buffered and already printed 2874 - * directly to the console when we received it, and 2875 - * record that has level above the console loglevel. 2876 - */ 2877 - console_seq++; 2878 - goto skip; 2879 - } 2880 - 2881 - /* Output to all consoles once old messages replayed. */ 2882 - if (unlikely(exclusive_console && 2883 - console_seq >= exclusive_console_stop_seq)) { 2884 - exclusive_console = NULL; 2885 - } 2886 - 2887 - /* 2888 - * Handle extended console text first because later 2889 - * record_print_text() will modify the record buffer in-place. 2890 - */ 2891 - if (nr_ext_console_drivers) { 2892 - ext_len = info_print_ext_header(ext_text, 2893 - sizeof(ext_text), 2894 - r.info); 2895 - ext_len += msg_print_ext_body(ext_text + ext_len, 2896 - sizeof(ext_text) - ext_len, 2897 - &r.text_buf[0], 2898 - r.info->text_len, 2899 - &r.info->dev_info); 2900 - } 2901 - len = record_print_text(&r, 2902 - console_msg_format & MSG_FORMAT_SYSLOG, 2903 - printk_time); 2904 - console_seq++; 2905 - 2752 + if (handover) { 2906 2753 /* 2907 2754 * While actively printing out messages, if another printk() 2908 2755 * were to occur on another CPU, it may wait for this one to ··· 2909 2770 printk_safe_enter_irqsave(flags); 2910 2771 console_lock_spinning_enable(); 2911 2772 2912 - stop_critical_timings(); /* don't trace print latency */ 2913 - call_console_drivers(ext_text, ext_len, text, len); 2914 - start_critical_timings(); 2915 - 2916 - handover = console_lock_spinning_disable_and_check(); 2917 - printk_safe_exit_irqrestore(flags); 2918 - if (handover) 2919 - return; 2920 - 2921 - /* Allow panic_cpu to take over the consoles safely */ 2922 - if (abandon_console_lock_in_panic()) 2923 - break; 2924 - 2925 - if (do_cond_resched) 2926 - cond_resched(); 2773 + /* don't trace irqsoff print latency */ 2774 + stop_critical_timings(); 2927 2775 } 2928 2776 2929 - /* Get consistent value of the next-to-be-used sequence number. */ 2930 - next_seq = console_seq; 2777 + call_console_driver(con, write_text, len, dropped_text); 2931 2778 2932 - console_locked = 0; 2933 - up_console_sem(); 2779 + con->seq++; 2780 + 2781 + if (handover) { 2782 + start_critical_timings(); 2783 + *handover = console_lock_spinning_disable_and_check(); 2784 + printk_safe_exit_irqrestore(flags); 2785 + } 2786 + skip: 2787 + return true; 2788 + } 2789 + 2790 + /* 2791 + * Print a record for a given console, but allow another printk() caller to 2792 + * take over the console_lock and continue printing. 2793 + * 2794 + * Requires the console_lock, but depending on @handover after the call, the 2795 + * caller may no longer have the console_lock. 2796 + * 2797 + * See __console_emit_next_record() for argument and return details. 2798 + */ 2799 + static bool console_emit_next_record_transferable(struct console *con, char *text, char *ext_text, 2800 + char *dropped_text, bool *handover) 2801 + { 2802 + /* 2803 + * Handovers are only supported if threaded printers are atomically 2804 + * blocked. The context taking over the console_lock may be atomic. 2805 + */ 2806 + if (!console_kthreads_atomically_blocked()) { 2807 + *handover = false; 2808 + handover = NULL; 2809 + } 2810 + 2811 + return __console_emit_next_record(con, text, ext_text, dropped_text, handover); 2812 + } 2813 + 2814 + /* 2815 + * Print out all remaining records to all consoles. 2816 + * 2817 + * @do_cond_resched is set by the caller. It can be true only in schedulable 2818 + * context. 2819 + * 2820 + * @next_seq is set to the sequence number after the last available record. 2821 + * The value is valid only when this function returns true. It means that all 2822 + * usable consoles are completely flushed. 2823 + * 2824 + * @handover will be set to true if a printk waiter has taken over the 2825 + * console_lock, in which case the caller is no longer holding the 2826 + * console_lock. Otherwise it is set to false. 2827 + * 2828 + * Returns true when there was at least one usable console and all messages 2829 + * were flushed to all usable consoles. A returned false informs the caller 2830 + * that everything was not flushed (either there were no usable consoles or 2831 + * another context has taken over printing or it is a panic situation and this 2832 + * is not the panic CPU or direct printing is not preferred). Regardless the 2833 + * reason, the caller should assume it is not useful to immediately try again. 2834 + * 2835 + * Requires the console_lock. 2836 + */ 2837 + static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover) 2838 + { 2839 + static char dropped_text[DROPPED_TEXT_MAX]; 2840 + static char ext_text[CONSOLE_EXT_LOG_MAX]; 2841 + static char text[CONSOLE_LOG_MAX]; 2842 + bool any_usable = false; 2843 + struct console *con; 2844 + bool any_progress; 2845 + 2846 + *next_seq = 0; 2847 + *handover = false; 2848 + 2849 + do { 2850 + /* Let the kthread printers do the work if they can. */ 2851 + if (!allow_direct_printing()) 2852 + return false; 2853 + 2854 + any_progress = false; 2855 + 2856 + for_each_console(con) { 2857 + bool progress; 2858 + 2859 + if (!console_is_usable(con)) 2860 + continue; 2861 + any_usable = true; 2862 + 2863 + if (con->flags & CON_EXTENDED) { 2864 + /* Extended consoles do not print "dropped messages". */ 2865 + progress = console_emit_next_record_transferable(con, &text[0], 2866 + &ext_text[0], NULL, handover); 2867 + } else { 2868 + progress = console_emit_next_record_transferable(con, &text[0], 2869 + NULL, &dropped_text[0], handover); 2870 + } 2871 + if (*handover) 2872 + return false; 2873 + 2874 + /* Track the next of the highest seq flushed. */ 2875 + if (con->seq > *next_seq) 2876 + *next_seq = con->seq; 2877 + 2878 + if (!progress) 2879 + continue; 2880 + any_progress = true; 2881 + 2882 + /* Allow panic_cpu to take over the consoles safely. */ 2883 + if (abandon_console_lock_in_panic()) 2884 + return false; 2885 + 2886 + if (do_cond_resched) 2887 + cond_resched(); 2888 + } 2889 + } while (any_progress); 2890 + 2891 + return any_usable; 2892 + } 2893 + 2894 + /** 2895 + * console_unlock - unlock the console system 2896 + * 2897 + * Releases the console_lock which the caller holds on the console system 2898 + * and the console driver list. 2899 + * 2900 + * While the console_lock was held, console output may have been buffered 2901 + * by printk(). If this is the case, console_unlock(); emits 2902 + * the output prior to releasing the lock. 2903 + * 2904 + * console_unlock(); may be called from any context. 2905 + */ 2906 + void console_unlock(void) 2907 + { 2908 + bool do_cond_resched; 2909 + bool handover; 2910 + bool flushed; 2911 + u64 next_seq; 2912 + 2913 + if (console_suspended) { 2914 + up_console_sem(); 2915 + return; 2916 + } 2934 2917 2935 2918 /* 2936 - * Someone could have filled up the buffer again, so re-check if there's 2937 - * something to flush. In case we cannot trylock the console_sem again, 2938 - * there's a new owner and the console_unlock() from them will do the 2939 - * flush, no worries. 2919 + * Console drivers are called with interrupts disabled, so 2920 + * @console_may_schedule should be cleared before; however, we may 2921 + * end up dumping a lot of lines, for example, if called from 2922 + * console registration path, and should invoke cond_resched() 2923 + * between lines if allowable. Not doing so can cause a very long 2924 + * scheduling stall on a slow console leading to RCU stall and 2925 + * softlockup warnings which exacerbate the issue with more 2926 + * messages practically incapacitating the system. Therefore, create 2927 + * a local to use for the printing loop. 2940 2928 */ 2941 - retry = prb_read_valid(prb, next_seq, NULL); 2942 - if (retry && !abandon_console_lock_in_panic() && console_trylock()) 2943 - goto again; 2929 + do_cond_resched = console_may_schedule; 2930 + 2931 + do { 2932 + console_may_schedule = 0; 2933 + 2934 + flushed = console_flush_all(do_cond_resched, &next_seq, &handover); 2935 + if (!handover) 2936 + __console_unlock(); 2937 + 2938 + /* 2939 + * Abort if there was a failure to flush all messages to all 2940 + * usable consoles. Either it is not possible to flush (in 2941 + * which case it would be an infinite loop of retrying) or 2942 + * another context has taken over printing. 2943 + */ 2944 + if (!flushed) 2945 + break; 2946 + 2947 + /* 2948 + * Some context may have added new records after 2949 + * console_flush_all() but before unlocking the console. 2950 + * Re-check if there is a new record to flush. If the trylock 2951 + * fails, another context is already handling the printing. 2952 + */ 2953 + } while (prb_read_valid(prb, next_seq, NULL) && console_trylock()); 2944 2954 } 2945 2955 EXPORT_SYMBOL(console_unlock); 2946 2956 ··· 3120 2832 if (oops_in_progress) { 3121 2833 if (down_trylock_console_sem() != 0) 3122 2834 return; 2835 + if (!console_kthreads_atomic_tryblock()) { 2836 + up_console_sem(); 2837 + return; 2838 + } 3123 2839 } else 3124 2840 console_lock(); 3125 2841 3126 - console_locked = 1; 3127 2842 console_may_schedule = 0; 3128 2843 for_each_console(c) 3129 2844 if ((c->flags & CON_ENABLED) && c->unblank) 3130 2845 c->unblank(); 3131 2846 console_unlock(); 2847 + 2848 + if (!oops_in_progress) 2849 + pr_flush(1000, true); 3132 2850 } 3133 2851 3134 2852 /** ··· 3155 2861 console_trylock(); 3156 2862 console_may_schedule = 0; 3157 2863 3158 - if (mode == CONSOLE_REPLAY_ALL) 3159 - console_seq = prb_first_valid_seq(prb); 2864 + if (mode == CONSOLE_REPLAY_ALL) { 2865 + struct console *c; 2866 + u64 seq; 2867 + 2868 + seq = prb_first_valid_seq(prb); 2869 + for_each_console(c) 2870 + c->seq = seq; 2871 + } 3160 2872 console_unlock(); 3161 2873 } 3162 2874 ··· 3193 2893 */ 3194 2894 void console_stop(struct console *console) 3195 2895 { 2896 + __pr_flush(console, 1000, true); 3196 2897 console_lock(); 3197 2898 console->flags &= ~CON_ENABLED; 3198 2899 console_unlock(); ··· 3205 2904 console_lock(); 3206 2905 console->flags |= CON_ENABLED; 3207 2906 console_unlock(); 2907 + __pr_flush(console, 1000, true); 3208 2908 } 3209 2909 EXPORT_SYMBOL(console_start); 3210 2910 ··· 3291 2989 if (newcon->device) 3292 2990 newcon->flags |= CON_CONSDEV; 3293 2991 } 2992 + 2993 + #define con_printk(lvl, con, fmt, ...) \ 2994 + printk(lvl pr_fmt("%sconsole [%s%d] " fmt), \ 2995 + (con->flags & CON_BOOT) ? "boot" : "", \ 2996 + con->name, con->index, ##__VA_ARGS__) 3294 2997 3295 2998 /* 3296 2999 * The console driver calls this routine during kernel initialization ··· 3404 3097 if (newcon->flags & CON_EXTENDED) 3405 3098 nr_ext_console_drivers++; 3406 3099 3407 - if (newcon->flags & CON_PRINTBUFFER) { 3408 - /* 3409 - * console_unlock(); will print out the buffered messages 3410 - * for us. 3411 - * 3412 - * We're about to replay the log buffer. Only do this to the 3413 - * just-registered console to avoid excessive message spam to 3414 - * the already-registered consoles. 3415 - * 3416 - * Set exclusive_console with disabled interrupts to reduce 3417 - * race window with eventual console_flush_on_panic() that 3418 - * ignores console_lock. 3419 - */ 3420 - exclusive_console = newcon; 3421 - exclusive_console_stop_seq = console_seq; 3100 + newcon->dropped = 0; 3101 + newcon->thread = NULL; 3102 + newcon->blocked = true; 3103 + mutex_init(&newcon->lock); 3422 3104 3105 + if (newcon->flags & CON_PRINTBUFFER) { 3423 3106 /* Get a consistent copy of @syslog_seq. */ 3424 3107 mutex_lock(&syslog_lock); 3425 - console_seq = syslog_seq; 3108 + newcon->seq = syslog_seq; 3426 3109 mutex_unlock(&syslog_lock); 3110 + } else { 3111 + /* Begin with next message. */ 3112 + newcon->seq = prb_next_seq(prb); 3427 3113 } 3114 + 3115 + if (printk_kthreads_available) 3116 + printk_start_kthread(newcon); 3117 + 3428 3118 console_unlock(); 3429 3119 console_sysfs_notify(); 3430 3120 ··· 3432 3128 * users know there might be something in the kernel's log buffer that 3433 3129 * went to the bootconsole (that they do not see on the real console) 3434 3130 */ 3435 - pr_info("%sconsole [%s%d] enabled\n", 3436 - (newcon->flags & CON_BOOT) ? "boot" : "" , 3437 - newcon->name, newcon->index); 3131 + con_printk(KERN_INFO, newcon, "enabled\n"); 3438 3132 if (bootcon_enabled && 3439 3133 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && 3440 3134 !keep_bootcon) { ··· 3448 3146 3449 3147 int unregister_console(struct console *console) 3450 3148 { 3149 + struct task_struct *thd; 3451 3150 struct console *con; 3452 3151 int res; 3453 3152 3454 - pr_info("%sconsole [%s%d] disabled\n", 3455 - (console->flags & CON_BOOT) ? "boot" : "" , 3456 - console->name, console->index); 3153 + con_printk(KERN_INFO, console, "disabled\n"); 3457 3154 3458 3155 res = _braille_unregister_console(console); 3459 3156 if (res < 0) ··· 3489 3188 console_drivers->flags |= CON_CONSDEV; 3490 3189 3491 3190 console->flags &= ~CON_ENABLED; 3191 + 3192 + /* 3193 + * console->thread can only be cleared under the console lock. But 3194 + * stopping the thread must be done without the console lock. The 3195 + * task that clears @thread is the task that stops the kthread. 3196 + */ 3197 + thd = console->thread; 3198 + console->thread = NULL; 3199 + 3492 3200 console_unlock(); 3201 + 3202 + if (thd) 3203 + kthread_stop(thd); 3204 + 3493 3205 console_sysfs_notify(); 3494 3206 3495 3207 if (console->exit) ··· 3598 3284 } 3599 3285 late_initcall(printk_late_init); 3600 3286 3287 + static int __init printk_activate_kthreads(void) 3288 + { 3289 + struct console *con; 3290 + 3291 + console_lock(); 3292 + printk_kthreads_available = true; 3293 + for_each_console(con) 3294 + printk_start_kthread(con); 3295 + console_unlock(); 3296 + 3297 + return 0; 3298 + } 3299 + early_initcall(printk_activate_kthreads); 3300 + 3601 3301 #if defined CONFIG_PRINTK 3302 + /* If @con is specified, only wait for that console. Otherwise wait for all. */ 3303 + static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) 3304 + { 3305 + int remaining = timeout_ms; 3306 + struct console *c; 3307 + u64 last_diff = 0; 3308 + u64 printk_seq; 3309 + u64 diff; 3310 + u64 seq; 3311 + 3312 + might_sleep(); 3313 + 3314 + seq = prb_next_seq(prb); 3315 + 3316 + for (;;) { 3317 + diff = 0; 3318 + 3319 + console_lock(); 3320 + for_each_console(c) { 3321 + if (con && con != c) 3322 + continue; 3323 + if (!console_is_usable(c)) 3324 + continue; 3325 + printk_seq = c->seq; 3326 + if (printk_seq < seq) 3327 + diff += seq - printk_seq; 3328 + } 3329 + console_unlock(); 3330 + 3331 + if (diff != last_diff && reset_on_progress) 3332 + remaining = timeout_ms; 3333 + 3334 + if (diff == 0 || remaining == 0) 3335 + break; 3336 + 3337 + if (remaining < 0) { 3338 + /* no timeout limit */ 3339 + msleep(100); 3340 + } else if (remaining < 100) { 3341 + msleep(remaining); 3342 + remaining = 0; 3343 + } else { 3344 + msleep(100); 3345 + remaining -= 100; 3346 + } 3347 + 3348 + last_diff = diff; 3349 + } 3350 + 3351 + return (diff == 0); 3352 + } 3353 + 3354 + /** 3355 + * pr_flush() - Wait for printing threads to catch up. 3356 + * 3357 + * @timeout_ms: The maximum time (in ms) to wait. 3358 + * @reset_on_progress: Reset the timeout if forward progress is seen. 3359 + * 3360 + * A value of 0 for @timeout_ms means no waiting will occur. A value of -1 3361 + * represents infinite waiting. 3362 + * 3363 + * If @reset_on_progress is true, the timeout will be reset whenever any 3364 + * printer has been seen to make some forward progress. 3365 + * 3366 + * Context: Process context. May sleep while acquiring console lock. 3367 + * Return: true if all enabled printers are caught up. 3368 + */ 3369 + bool pr_flush(int timeout_ms, bool reset_on_progress) 3370 + { 3371 + return __pr_flush(NULL, timeout_ms, reset_on_progress); 3372 + } 3373 + EXPORT_SYMBOL(pr_flush); 3374 + 3375 + static void __printk_fallback_preferred_direct(void) 3376 + { 3377 + printk_prefer_direct_enter(); 3378 + pr_err("falling back to preferred direct printing\n"); 3379 + printk_kthreads_available = false; 3380 + } 3381 + 3382 + /* 3383 + * Enter preferred direct printing, but never exit. Mark console threads as 3384 + * unavailable. The system is then forever in preferred direct printing and 3385 + * any printing threads will exit. 3386 + * 3387 + * Must *not* be called under console_lock. Use 3388 + * __printk_fallback_preferred_direct() if already holding console_lock. 3389 + */ 3390 + static void printk_fallback_preferred_direct(void) 3391 + { 3392 + console_lock(); 3393 + __printk_fallback_preferred_direct(); 3394 + console_unlock(); 3395 + } 3396 + 3397 + /* 3398 + * Print a record for a given console, not allowing another printk() caller 3399 + * to take over. This is appropriate for contexts that do not have the 3400 + * console_lock. 3401 + * 3402 + * See __console_emit_next_record() for argument and return details. 3403 + */ 3404 + static bool console_emit_next_record(struct console *con, char *text, char *ext_text, 3405 + char *dropped_text) 3406 + { 3407 + return __console_emit_next_record(con, text, ext_text, dropped_text, NULL); 3408 + } 3409 + 3410 + static bool printer_should_wake(struct console *con, u64 seq) 3411 + { 3412 + short flags; 3413 + 3414 + if (kthread_should_stop() || !printk_kthreads_available) 3415 + return true; 3416 + 3417 + if (con->blocked || 3418 + console_kthreads_atomically_blocked()) { 3419 + return false; 3420 + } 3421 + 3422 + /* 3423 + * This is an unsafe read from con->flags, but a false positive is 3424 + * not a problem. Worst case it would allow the printer to wake up 3425 + * although it is disabled. But the printer will notice that when 3426 + * attempting to print and instead go back to sleep. 3427 + */ 3428 + flags = data_race(READ_ONCE(con->flags)); 3429 + 3430 + if (!__console_is_usable(flags)) 3431 + return false; 3432 + 3433 + return prb_read_valid(prb, seq, NULL); 3434 + } 3435 + 3436 + static int printk_kthread_func(void *data) 3437 + { 3438 + struct console *con = data; 3439 + char *dropped_text = NULL; 3440 + char *ext_text = NULL; 3441 + u64 seq = 0; 3442 + char *text; 3443 + int error; 3444 + 3445 + text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL); 3446 + if (!text) { 3447 + con_printk(KERN_ERR, con, "failed to allocate text buffer\n"); 3448 + printk_fallback_preferred_direct(); 3449 + goto out; 3450 + } 3451 + 3452 + if (con->flags & CON_EXTENDED) { 3453 + ext_text = kmalloc(CONSOLE_EXT_LOG_MAX, GFP_KERNEL); 3454 + if (!ext_text) { 3455 + con_printk(KERN_ERR, con, "failed to allocate ext_text buffer\n"); 3456 + printk_fallback_preferred_direct(); 3457 + goto out; 3458 + } 3459 + } else { 3460 + dropped_text = kmalloc(DROPPED_TEXT_MAX, GFP_KERNEL); 3461 + if (!dropped_text) { 3462 + con_printk(KERN_ERR, con, "failed to allocate dropped_text buffer\n"); 3463 + printk_fallback_preferred_direct(); 3464 + goto out; 3465 + } 3466 + } 3467 + 3468 + con_printk(KERN_INFO, con, "printing thread started\n"); 3469 + 3470 + for (;;) { 3471 + /* 3472 + * Guarantee this task is visible on the waitqueue before 3473 + * checking the wake condition. 3474 + * 3475 + * The full memory barrier within set_current_state() of 3476 + * prepare_to_wait_event() pairs with the full memory barrier 3477 + * within wq_has_sleeper(). 3478 + * 3479 + * This pairs with __wake_up_klogd:A. 3480 + */ 3481 + error = wait_event_interruptible(log_wait, 3482 + printer_should_wake(con, seq)); /* LMM(printk_kthread_func:A) */ 3483 + 3484 + if (kthread_should_stop() || !printk_kthreads_available) 3485 + break; 3486 + 3487 + if (error) 3488 + continue; 3489 + 3490 + error = mutex_lock_interruptible(&con->lock); 3491 + if (error) 3492 + continue; 3493 + 3494 + if (con->blocked || 3495 + !console_kthread_printing_tryenter()) { 3496 + /* Another context has locked the console_lock. */ 3497 + mutex_unlock(&con->lock); 3498 + continue; 3499 + } 3500 + 3501 + /* 3502 + * Although this context has not locked the console_lock, it 3503 + * is known that the console_lock is not locked and it is not 3504 + * possible for any other context to lock the console_lock. 3505 + * Therefore it is safe to read con->flags. 3506 + */ 3507 + 3508 + if (!__console_is_usable(con->flags)) { 3509 + console_kthread_printing_exit(); 3510 + mutex_unlock(&con->lock); 3511 + continue; 3512 + } 3513 + 3514 + /* 3515 + * Even though the printk kthread is always preemptible, it is 3516 + * still not allowed to call cond_resched() from within 3517 + * console drivers. The task may become non-preemptible in the 3518 + * console driver call chain. For example, vt_console_print() 3519 + * takes a spinlock and then can call into fbcon_redraw(), 3520 + * which can conditionally invoke cond_resched(). 3521 + */ 3522 + console_may_schedule = 0; 3523 + console_emit_next_record(con, text, ext_text, dropped_text); 3524 + 3525 + seq = con->seq; 3526 + 3527 + console_kthread_printing_exit(); 3528 + 3529 + mutex_unlock(&con->lock); 3530 + } 3531 + 3532 + con_printk(KERN_INFO, con, "printing thread stopped\n"); 3533 + out: 3534 + kfree(dropped_text); 3535 + kfree(ext_text); 3536 + kfree(text); 3537 + 3538 + console_lock(); 3539 + /* 3540 + * If this kthread is being stopped by another task, con->thread will 3541 + * already be NULL. That is fine. The important thing is that it is 3542 + * NULL after the kthread exits. 3543 + */ 3544 + con->thread = NULL; 3545 + console_unlock(); 3546 + 3547 + return 0; 3548 + } 3549 + 3550 + /* Must be called under console_lock. */ 3551 + static void printk_start_kthread(struct console *con) 3552 + { 3553 + /* 3554 + * Do not start a kthread if there is no write() callback. The 3555 + * kthreads assume the write() callback exists. 3556 + */ 3557 + if (!con->write) 3558 + return; 3559 + 3560 + con->thread = kthread_run(printk_kthread_func, con, 3561 + "pr/%s%d", con->name, con->index); 3562 + if (IS_ERR(con->thread)) { 3563 + con->thread = NULL; 3564 + con_printk(KERN_ERR, con, "unable to start printing thread\n"); 3565 + __printk_fallback_preferred_direct(); 3566 + return; 3567 + } 3568 + } 3569 + 3602 3570 /* 3603 3571 * Delayed printk version, for scheduler-internal messages: 3604 3572 */ 3605 - #define PRINTK_PENDING_WAKEUP 0x01 3606 - #define PRINTK_PENDING_OUTPUT 0x02 3573 + #define PRINTK_PENDING_WAKEUP 0x01 3574 + #define PRINTK_PENDING_DIRECT_OUTPUT 0x02 3607 3575 3608 3576 static DEFINE_PER_CPU(int, printk_pending); 3609 3577 ··· 3893 3297 { 3894 3298 int pending = this_cpu_xchg(printk_pending, 0); 3895 3299 3896 - if (pending & PRINTK_PENDING_OUTPUT) { 3300 + if (pending & PRINTK_PENDING_DIRECT_OUTPUT) { 3301 + printk_prefer_direct_enter(); 3302 + 3897 3303 /* If trylock fails, someone else is doing the printing */ 3898 3304 if (console_trylock()) 3899 3305 console_unlock(); 3306 + 3307 + printk_prefer_direct_exit(); 3900 3308 } 3901 3309 3902 3310 if (pending & PRINTK_PENDING_WAKEUP) 3903 - wake_up_interruptible(&log_wait); 3311 + wake_up_interruptible_all(&log_wait); 3904 3312 } 3905 3313 3906 3314 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = 3907 3315 IRQ_WORK_INIT_LAZY(wake_up_klogd_work_func); 3908 3316 3909 - void wake_up_klogd(void) 3317 + static void __wake_up_klogd(int val) 3910 3318 { 3911 3319 if (!printk_percpu_data_ready()) 3912 3320 return; 3913 3321 3914 3322 preempt_disable(); 3915 - if (waitqueue_active(&log_wait)) { 3916 - this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); 3323 + /* 3324 + * Guarantee any new records can be seen by tasks preparing to wait 3325 + * before this context checks if the wait queue is empty. 3326 + * 3327 + * The full memory barrier within wq_has_sleeper() pairs with the full 3328 + * memory barrier within set_current_state() of 3329 + * prepare_to_wait_event(), which is called after ___wait_event() adds 3330 + * the waiter but before it has checked the wait condition. 3331 + * 3332 + * This pairs with devkmsg_read:A, syslog_print:A, and 3333 + * printk_kthread_func:A. 3334 + */ 3335 + if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */ 3336 + (val & PRINTK_PENDING_DIRECT_OUTPUT)) { 3337 + this_cpu_or(printk_pending, val); 3917 3338 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); 3918 3339 } 3919 3340 preempt_enable(); 3920 3341 } 3921 3342 3343 + void wake_up_klogd(void) 3344 + { 3345 + __wake_up_klogd(PRINTK_PENDING_WAKEUP); 3346 + } 3347 + 3922 3348 void defer_console_output(void) 3923 3349 { 3924 - if (!printk_percpu_data_ready()) 3925 - return; 3350 + /* 3351 + * New messages may have been added directly to the ringbuffer 3352 + * using vprintk_store(), so wake any waiters as well. 3353 + */ 3354 + int val = PRINTK_PENDING_WAKEUP; 3926 3355 3927 - preempt_disable(); 3928 - this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT); 3929 - irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); 3930 - preempt_enable(); 3356 + /* 3357 + * Make sure that some context will print the messages when direct 3358 + * printing is allowed. This happens in situations when the kthreads 3359 + * may not be as reliable or perhaps unusable. 3360 + */ 3361 + if (allow_direct_printing()) 3362 + val |= PRINTK_PENDING_DIRECT_OUTPUT; 3363 + 3364 + __wake_up_klogd(val); 3931 3365 } 3932 3366 3933 3367 void printk_trigger_flush(void) ··· 4293 3667 #endif 4294 3668 4295 3669 #ifdef CONFIG_SMP 4296 - static atomic_t printk_cpulock_owner = ATOMIC_INIT(-1); 4297 - static atomic_t printk_cpulock_nested = ATOMIC_INIT(0); 3670 + static atomic_t printk_cpu_sync_owner = ATOMIC_INIT(-1); 3671 + static atomic_t printk_cpu_sync_nested = ATOMIC_INIT(0); 4298 3672 4299 3673 /** 4300 - * __printk_wait_on_cpu_lock() - Busy wait until the printk cpu-reentrant 4301 - * spinning lock is not owned by any CPU. 3674 + * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant 3675 + * spinning lock is not owned by any CPU. 4302 3676 * 4303 3677 * Context: Any context. 4304 3678 */ 4305 - void __printk_wait_on_cpu_lock(void) 3679 + void __printk_cpu_sync_wait(void) 4306 3680 { 4307 3681 do { 4308 3682 cpu_relax(); 4309 - } while (atomic_read(&printk_cpulock_owner) != -1); 3683 + } while (atomic_read(&printk_cpu_sync_owner) != -1); 4310 3684 } 4311 - EXPORT_SYMBOL(__printk_wait_on_cpu_lock); 3685 + EXPORT_SYMBOL(__printk_cpu_sync_wait); 4312 3686 4313 3687 /** 4314 - * __printk_cpu_trylock() - Try to acquire the printk cpu-reentrant 4315 - * spinning lock. 3688 + * __printk_cpu_sync_try_get() - Try to acquire the printk cpu-reentrant 3689 + * spinning lock. 4316 3690 * 4317 3691 * If no processor has the lock, the calling processor takes the lock and 4318 3692 * becomes the owner. If the calling processor is already the owner of the ··· 4321 3695 * Context: Any context. Expects interrupts to be disabled. 4322 3696 * Return: 1 on success, otherwise 0. 4323 3697 */ 4324 - int __printk_cpu_trylock(void) 3698 + int __printk_cpu_sync_try_get(void) 4325 3699 { 4326 3700 int cpu; 4327 3701 int old; ··· 4331 3705 /* 4332 3706 * Guarantee loads and stores from this CPU when it is the lock owner 4333 3707 * are _not_ visible to the previous lock owner. This pairs with 4334 - * __printk_cpu_unlock:B. 3708 + * __printk_cpu_sync_put:B. 4335 3709 * 4336 3710 * Memory barrier involvement: 4337 3711 * 4338 - * If __printk_cpu_trylock:A reads from __printk_cpu_unlock:B, then 4339 - * __printk_cpu_unlock:A can never read from __printk_cpu_trylock:B. 3712 + * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B, 3713 + * then __printk_cpu_sync_put:A can never read from 3714 + * __printk_cpu_sync_try_get:B. 4340 3715 * 4341 3716 * Relies on: 4342 3717 * 4343 - * RELEASE from __printk_cpu_unlock:A to __printk_cpu_unlock:B 3718 + * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B 4344 3719 * of the previous CPU 4345 3720 * matching 4346 - * ACQUIRE from __printk_cpu_trylock:A to __printk_cpu_trylock:B 4347 - * of this CPU 3721 + * ACQUIRE from __printk_cpu_sync_try_get:A to 3722 + * __printk_cpu_sync_try_get:B of this CPU 4348 3723 */ 4349 - old = atomic_cmpxchg_acquire(&printk_cpulock_owner, -1, 4350 - cpu); /* LMM(__printk_cpu_trylock:A) */ 3724 + old = atomic_cmpxchg_acquire(&printk_cpu_sync_owner, -1, 3725 + cpu); /* LMM(__printk_cpu_sync_try_get:A) */ 4351 3726 if (old == -1) { 4352 3727 /* 4353 3728 * This CPU is now the owner and begins loading/storing 4354 - * data: LMM(__printk_cpu_trylock:B) 3729 + * data: LMM(__printk_cpu_sync_try_get:B) 4355 3730 */ 4356 3731 return 1; 4357 3732 4358 3733 } else if (old == cpu) { 4359 3734 /* This CPU is already the owner. */ 4360 - atomic_inc(&printk_cpulock_nested); 3735 + atomic_inc(&printk_cpu_sync_nested); 4361 3736 return 1; 4362 3737 } 4363 3738 4364 3739 return 0; 4365 3740 } 4366 - EXPORT_SYMBOL(__printk_cpu_trylock); 3741 + EXPORT_SYMBOL(__printk_cpu_sync_try_get); 4367 3742 4368 3743 /** 4369 - * __printk_cpu_unlock() - Release the printk cpu-reentrant spinning lock. 3744 + * __printk_cpu_sync_put() - Release the printk cpu-reentrant spinning lock. 4370 3745 * 4371 3746 * The calling processor must be the owner of the lock. 4372 3747 * 4373 3748 * Context: Any context. Expects interrupts to be disabled. 4374 3749 */ 4375 - void __printk_cpu_unlock(void) 3750 + void __printk_cpu_sync_put(void) 4376 3751 { 4377 - if (atomic_read(&printk_cpulock_nested)) { 4378 - atomic_dec(&printk_cpulock_nested); 3752 + if (atomic_read(&printk_cpu_sync_nested)) { 3753 + atomic_dec(&printk_cpu_sync_nested); 4379 3754 return; 4380 3755 } 4381 3756 4382 3757 /* 4383 3758 * This CPU is finished loading/storing data: 4384 - * LMM(__printk_cpu_unlock:A) 3759 + * LMM(__printk_cpu_sync_put:A) 4385 3760 */ 4386 3761 4387 3762 /* 4388 3763 * Guarantee loads and stores from this CPU when it was the 4389 3764 * lock owner are visible to the next lock owner. This pairs 4390 - * with __printk_cpu_trylock:A. 3765 + * with __printk_cpu_sync_try_get:A. 4391 3766 * 4392 3767 * Memory barrier involvement: 4393 3768 * 4394 - * If __printk_cpu_trylock:A reads from __printk_cpu_unlock:B, 4395 - * then __printk_cpu_trylock:B reads from __printk_cpu_unlock:A. 3769 + * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B, 3770 + * then __printk_cpu_sync_try_get:B reads from __printk_cpu_sync_put:A. 4396 3771 * 4397 3772 * Relies on: 4398 3773 * 4399 - * RELEASE from __printk_cpu_unlock:A to __printk_cpu_unlock:B 3774 + * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B 4400 3775 * of this CPU 4401 3776 * matching 4402 - * ACQUIRE from __printk_cpu_trylock:A to __printk_cpu_trylock:B 4403 - * of the next CPU 3777 + * ACQUIRE from __printk_cpu_sync_try_get:A to 3778 + * __printk_cpu_sync_try_get:B of the next CPU 4404 3779 */ 4405 - atomic_set_release(&printk_cpulock_owner, 4406 - -1); /* LMM(__printk_cpu_unlock:B) */ 3780 + atomic_set_release(&printk_cpu_sync_owner, 3781 + -1); /* LMM(__printk_cpu_sync_put:B) */ 4407 3782 } 4408 - EXPORT_SYMBOL(__printk_cpu_unlock); 3783 + EXPORT_SYMBOL(__printk_cpu_sync_put); 4409 3784 #endif /* CONFIG_SMP */
+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(); 650 651 trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected")); 651 652 pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name); 652 653 raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags); ··· 685 684 */ 686 685 set_tsk_need_resched(current); 687 686 set_preempt_need_resched(); 687 + printk_prefer_direct_exit(); 688 688 } 689 689 690 690 static void check_cpu_stall(struct rcu_data *rdp)
+13 -1
kernel/reboot.c
··· 447 447 ret = run_cmd(reboot_cmd); 448 448 449 449 if (ret) { 450 + printk_prefer_direct_enter(); 450 451 pr_warn("Failed to start orderly reboot: forcing the issue\n"); 451 452 emergency_sync(); 452 453 kernel_restart(NULL); 454 + printk_prefer_direct_exit(); 453 455 } 454 456 455 457 return ret; ··· 464 462 ret = run_cmd(poweroff_cmd); 465 463 466 464 if (ret && force) { 465 + printk_prefer_direct_enter(); 467 466 pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 468 467 469 468 /* ··· 474 471 */ 475 472 emergency_sync(); 476 473 kernel_power_off(); 474 + printk_prefer_direct_exit(); 477 475 } 478 476 479 477 return ret; ··· 532 528 */ 533 529 static void hw_failure_emergency_poweroff_func(struct work_struct *work) 534 530 { 531 + printk_prefer_direct_enter(); 532 + 535 533 /* 536 534 * We have reached here after the emergency shutdown waiting period has 537 535 * expired. This means orderly_poweroff has not been able to shut off ··· 550 544 */ 551 545 pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n"); 552 546 emergency_restart(); 547 + 548 + printk_prefer_direct_exit(); 553 549 } 554 550 555 551 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, ··· 590 582 { 591 583 static atomic_t allow_proceed = ATOMIC_INIT(1); 592 584 585 + printk_prefer_direct_enter(); 586 + 593 587 pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); 594 588 595 589 /* Shutdown should be initiated only once. */ 596 590 if (!atomic_dec_and_test(&allow_proceed)) 597 - return; 591 + goto out; 598 592 599 593 /* 600 594 * Queue a backup emergency shutdown in the event of ··· 604 594 */ 605 595 hw_failure_emergency_poweroff(ms_until_forced); 606 596 orderly_poweroff(true); 597 + out: 598 + printk_prefer_direct_exit(); 607 599 } 608 600 EXPORT_SYMBOL_GPL(hw_protection_shutdown); 609 601
+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 + 427 429 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", 428 430 smp_processor_id(), duration, 429 431 current->comm, task_pid_nr(current)); ··· 444 442 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); 445 443 if (softlockup_panic) 446 444 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 + 138 140 pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", 139 141 this_cpu); 140 142 print_modules(); ··· 156 154 157 155 if (hardlockup_panic) 158 156 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;
+2 -2
lib/dump_stack.c
··· 102 102 * Permit this cpu to perform nested stack dumps while serialising 103 103 * against other CPUs 104 104 */ 105 - printk_cpu_lock_irqsave(flags); 105 + printk_cpu_sync_get_irqsave(flags); 106 106 __dump_stack(log_lvl); 107 - printk_cpu_unlock_irqrestore(flags); 107 + printk_cpu_sync_put_irqrestore(flags); 108 108 } 109 109 EXPORT_SYMBOL(dump_stack_lvl); 110 110
+2 -2
lib/nmi_backtrace.c
··· 99 99 * Allow nested NMI backtraces while serializing 100 100 * against other CPUs. 101 101 */ 102 - printk_cpu_lock_irqsave(flags); 102 + printk_cpu_sync_get_irqsave(flags); 103 103 if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) { 104 104 pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n", 105 105 cpu, (void *)instruction_pointer(regs)); ··· 110 110 else 111 111 dump_stack(); 112 112 } 113 - printk_cpu_unlock_irqrestore(flags); 113 + printk_cpu_sync_put_irqrestore(flags); 114 114 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); 115 115 return true; 116 116 }