.. | .. |
---|
31 | 31 | #include <linux/bug.h> |
---|
32 | 32 | #include <linux/ratelimit.h> |
---|
33 | 33 | #include <linux/debugfs.h> |
---|
| 34 | +#include <linux/sysfs.h> |
---|
34 | 35 | #include <asm/sections.h> |
---|
35 | 36 | |
---|
36 | 37 | #define PANIC_TIMER_STEP 100 |
---|
.. | .. |
---|
41 | 42 | * Should we dump all CPUs backtraces in an oops event? |
---|
42 | 43 | * Defaults to 0, can be changed via sysctl. |
---|
43 | 44 | */ |
---|
44 | | -unsigned int __read_mostly sysctl_oops_all_cpu_backtrace; |
---|
| 45 | +static unsigned int __read_mostly sysctl_oops_all_cpu_backtrace; |
---|
| 46 | +#else |
---|
| 47 | +#define sysctl_oops_all_cpu_backtrace 0 |
---|
45 | 48 | #endif /* CONFIG_SMP */ |
---|
46 | 49 | |
---|
47 | 50 | int panic_on_oops = CONFIG_PANIC_ON_OOPS_VALUE; |
---|
.. | .. |
---|
54 | 57 | int panic_on_warn __read_mostly; |
---|
55 | 58 | unsigned long panic_on_taint; |
---|
56 | 59 | bool panic_on_taint_nousertaint = false; |
---|
| 60 | +static unsigned int warn_limit __read_mostly; |
---|
57 | 61 | |
---|
58 | 62 | int panic_timeout = CONFIG_PANIC_TIMEOUT; |
---|
59 | 63 | EXPORT_SYMBOL_GPL(panic_timeout); |
---|
.. | .. |
---|
69 | 73 | ATOMIC_NOTIFIER_HEAD(panic_notifier_list); |
---|
70 | 74 | |
---|
71 | 75 | EXPORT_SYMBOL(panic_notifier_list); |
---|
| 76 | + |
---|
| 77 | +#ifdef CONFIG_SYSCTL |
---|
| 78 | +static struct ctl_table kern_panic_table[] = { |
---|
| 79 | +#ifdef CONFIG_SMP |
---|
| 80 | + { |
---|
| 81 | + .procname = "oops_all_cpu_backtrace", |
---|
| 82 | + .data = &sysctl_oops_all_cpu_backtrace, |
---|
| 83 | + .maxlen = sizeof(int), |
---|
| 84 | + .mode = 0644, |
---|
| 85 | + .proc_handler = proc_dointvec_minmax, |
---|
| 86 | + .extra1 = SYSCTL_ZERO, |
---|
| 87 | + .extra2 = SYSCTL_ONE, |
---|
| 88 | + }, |
---|
| 89 | +#endif |
---|
| 90 | + { |
---|
| 91 | + .procname = "warn_limit", |
---|
| 92 | + .data = &warn_limit, |
---|
| 93 | + .maxlen = sizeof(warn_limit), |
---|
| 94 | + .mode = 0644, |
---|
| 95 | + .proc_handler = proc_douintvec, |
---|
| 96 | + }, |
---|
| 97 | + { } |
---|
| 98 | +}; |
---|
| 99 | + |
---|
| 100 | +static __init int kernel_panic_sysctls_init(void) |
---|
| 101 | +{ |
---|
| 102 | + register_sysctl_init("kernel", kern_panic_table); |
---|
| 103 | + return 0; |
---|
| 104 | +} |
---|
| 105 | +late_initcall(kernel_panic_sysctls_init); |
---|
| 106 | +#endif |
---|
| 107 | + |
---|
| 108 | +static atomic_t warn_count = ATOMIC_INIT(0); |
---|
| 109 | + |
---|
| 110 | +#ifdef CONFIG_SYSFS |
---|
| 111 | +static ssize_t warn_count_show(struct kobject *kobj, struct kobj_attribute *attr, |
---|
| 112 | + char *page) |
---|
| 113 | +{ |
---|
| 114 | + return sysfs_emit(page, "%d\n", atomic_read(&warn_count)); |
---|
| 115 | +} |
---|
| 116 | + |
---|
| 117 | +static struct kobj_attribute warn_count_attr = __ATTR_RO(warn_count); |
---|
| 118 | + |
---|
| 119 | +static __init int kernel_panic_sysfs_init(void) |
---|
| 120 | +{ |
---|
| 121 | + sysfs_add_file_to_group(kernel_kobj, &warn_count_attr.attr, NULL); |
---|
| 122 | + return 0; |
---|
| 123 | +} |
---|
| 124 | +late_initcall(kernel_panic_sysfs_init); |
---|
| 125 | +#endif |
---|
72 | 126 | |
---|
73 | 127 | static long no_blink(int state) |
---|
74 | 128 | { |
---|
.. | .. |
---|
166 | 220 | ftrace_dump(DUMP_ALL); |
---|
167 | 221 | } |
---|
168 | 222 | |
---|
| 223 | +void check_panic_on_warn(const char *origin) |
---|
| 224 | +{ |
---|
| 225 | + unsigned int limit; |
---|
| 226 | + |
---|
| 227 | + if (panic_on_warn) |
---|
| 228 | + panic("%s: panic_on_warn set ...\n", origin); |
---|
| 229 | + |
---|
| 230 | + limit = READ_ONCE(warn_limit); |
---|
| 231 | + if (atomic_inc_return(&warn_count) >= limit && limit) |
---|
| 232 | + panic("%s: system warned too often (kernel.warn_limit is %d)", |
---|
| 233 | + origin, limit); |
---|
| 234 | +} |
---|
| 235 | + |
---|
169 | 236 | /** |
---|
170 | 237 | * panic - halt the system |
---|
171 | 238 | * @fmt: The text string to print |
---|
.. | .. |
---|
177 | 244 | void panic(const char *fmt, ...) |
---|
178 | 245 | { |
---|
179 | 246 | static char buf[1024]; |
---|
180 | | - va_list args2; |
---|
181 | 247 | va_list args; |
---|
182 | 248 | long i, i_next = 0, len; |
---|
183 | 249 | int state = 0; |
---|
184 | 250 | int old_cpu, this_cpu; |
---|
185 | 251 | bool _crash_kexec_post_notifiers = crash_kexec_post_notifiers; |
---|
| 252 | + |
---|
| 253 | + if (panic_on_warn) { |
---|
| 254 | + /* |
---|
| 255 | + * This thread may hit another WARN() in the panic path. |
---|
| 256 | + * Resetting this prevents additional WARN() from panicking the |
---|
| 257 | + * system on this thread. Other threads are blocked by the |
---|
| 258 | + * panic_mutex in panic(). |
---|
| 259 | + */ |
---|
| 260 | + panic_on_warn = 0; |
---|
| 261 | + } |
---|
186 | 262 | |
---|
187 | 263 | /* |
---|
188 | 264 | * Disable local interrupts. This will prevent panic_smp_self_stop |
---|
.. | .. |
---|
192 | 268 | */ |
---|
193 | 269 | local_irq_disable(); |
---|
194 | 270 | preempt_disable_notrace(); |
---|
195 | | - |
---|
196 | | - console_verbose(); |
---|
197 | | - pr_emerg("Kernel panic - not syncing:\n"); |
---|
198 | | - va_start(args2, fmt); |
---|
199 | | - va_copy(args, args2); |
---|
200 | | - vprintk(fmt, args2); |
---|
201 | | - va_end(args2); |
---|
202 | | -#ifdef CONFIG_DEBUG_BUGVERBOSE |
---|
203 | | - /* |
---|
204 | | - * Avoid nested stack-dumping if a panic occurs during oops processing |
---|
205 | | - */ |
---|
206 | | - if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) |
---|
207 | | - dump_stack(); |
---|
208 | | -#endif |
---|
209 | | - pr_flush(1000, true); |
---|
210 | 271 | |
---|
211 | 272 | /* |
---|
212 | 273 | * It's possible to come here directly from a panic-assertion and |
---|
.. | .. |
---|
229 | 290 | if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu) |
---|
230 | 291 | panic_smp_self_stop(); |
---|
231 | 292 | |
---|
| 293 | + console_verbose(); |
---|
232 | 294 | bust_spinlocks(1); |
---|
| 295 | + va_start(args, fmt); |
---|
233 | 296 | len = vscnprintf(buf, sizeof(buf), fmt, args); |
---|
234 | 297 | va_end(args); |
---|
235 | 298 | |
---|
236 | 299 | if (len && buf[len - 1] == '\n') |
---|
237 | 300 | buf[len - 1] = '\0'; |
---|
| 301 | + |
---|
| 302 | + pr_emerg("Kernel panic - not syncing: %s\n", buf); |
---|
| 303 | +#ifdef CONFIG_DEBUG_BUGVERBOSE |
---|
| 304 | + /* |
---|
| 305 | + * Avoid nested stack-dumping if a panic occurs during oops processing |
---|
| 306 | + */ |
---|
| 307 | + if (!test_taint(TAINT_DIE) && oops_in_progress <= 1) |
---|
| 308 | + dump_stack(); |
---|
| 309 | +#endif |
---|
238 | 310 | |
---|
239 | 311 | /* |
---|
240 | 312 | * If kgdb is enabled, give it a chance to run before we stop all |
---|
.. | .. |
---|
252 | 324 | * Bypass the panic_cpu check and call __crash_kexec directly. |
---|
253 | 325 | */ |
---|
254 | 326 | if (!_crash_kexec_post_notifiers) { |
---|
| 327 | + printk_safe_flush_on_panic(); |
---|
255 | 328 | __crash_kexec(NULL); |
---|
256 | 329 | |
---|
257 | 330 | /* |
---|
.. | .. |
---|
275 | 348 | */ |
---|
276 | 349 | atomic_notifier_call_chain(&panic_notifier_list, 0, buf); |
---|
277 | 350 | |
---|
| 351 | + /* Call flush even twice. It tries harder with a single online CPU */ |
---|
| 352 | + printk_safe_flush_on_panic(); |
---|
278 | 353 | kmsg_dump(KMSG_DUMP_PANIC); |
---|
279 | 354 | |
---|
280 | 355 | /* |
---|
.. | .. |
---|
544 | 619 | |
---|
545 | 620 | static int init_oops_id(void) |
---|
546 | 621 | { |
---|
547 | | -#ifndef CONFIG_PREEMPT_RT |
---|
548 | 622 | if (!oops_id) |
---|
549 | 623 | get_random_bytes(&oops_id, sizeof(oops_id)); |
---|
550 | 624 | else |
---|
551 | | -#endif |
---|
552 | 625 | oops_id++; |
---|
553 | 626 | |
---|
554 | 627 | return 0; |
---|
.. | .. |
---|
559 | 632 | { |
---|
560 | 633 | init_oops_id(); |
---|
561 | 634 | pr_warn("---[ end trace %016llx ]---\n", (unsigned long long)oops_id); |
---|
562 | | - pr_flush(1000, true); |
---|
563 | 635 | } |
---|
564 | 636 | |
---|
565 | 637 | /* |
---|
.. | .. |
---|
599 | 671 | if (regs) |
---|
600 | 672 | show_regs(regs); |
---|
601 | 673 | |
---|
602 | | - if (panic_on_warn) { |
---|
603 | | - /* |
---|
604 | | - * This thread may hit another WARN() in the panic path. |
---|
605 | | - * Resetting this prevents additional WARN() from panicking the |
---|
606 | | - * system on this thread. Other threads are blocked by the |
---|
607 | | - * panic_mutex in panic(). |
---|
608 | | - */ |
---|
609 | | - panic_on_warn = 0; |
---|
610 | | - panic("panic_on_warn set ...\n"); |
---|
611 | | - } |
---|
| 674 | + check_panic_on_warn("kernel"); |
---|
612 | 675 | |
---|
613 | 676 | if (!regs) |
---|
614 | 677 | dump_stack(); |
---|