Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/smp.h> |
| 4 | #include <linux/reboot.h> |
| 5 | #include <linux/kexec.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 6 | #include <linux/memblock.h> |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 7 | #include <linux/crash_dump.h> |
| 8 | #include <linux/delay.h> |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 9 | #include <linux/irq.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 12 | #include <linux/sched/task_stack.h> |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 13 | |
| 14 | /* This keeps a track of which one is crashing cpu. */ |
| 15 | static int crashing_cpu = -1; |
| 16 | static cpumask_t cpus_in_crash = CPU_MASK_NONE; |
| 17 | |
| 18 | #ifdef CONFIG_SMP |
Corey Minyard | c80e1b6 | 2016-04-11 09:10:19 -0500 | [diff] [blame] | 19 | static void crash_shutdown_secondary(void *passed_regs) |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 20 | { |
Corey Minyard | c80e1b6 | 2016-04-11 09:10:19 -0500 | [diff] [blame] | 21 | struct pt_regs *regs = passed_regs; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 22 | int cpu = smp_processor_id(); |
| 23 | |
Corey Minyard | c80e1b6 | 2016-04-11 09:10:19 -0500 | [diff] [blame] | 24 | /* |
| 25 | * If we are passed registers, use those. Otherwise get the |
| 26 | * regs from the last interrupt, which should be correct, as |
| 27 | * we are in an interrupt. But if the regs are not there, |
| 28 | * pull them from the top of the stack. They are probably |
| 29 | * wrong, but we need something to keep from crashing again. |
| 30 | */ |
| 31 | if (!regs) |
| 32 | regs = get_irq_regs(); |
| 33 | if (!regs) |
| 34 | regs = task_pt_regs(current); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 35 | |
| 36 | if (!cpu_online(cpu)) |
| 37 | return; |
| 38 | |
Dengcheng Zhu | dc57aaf | 2018-09-11 14:49:20 -0700 | [diff] [blame] | 39 | /* We won't be sent IPIs any more. */ |
| 40 | set_cpu_online(cpu, false); |
| 41 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 42 | local_irq_disable(); |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 43 | if (!cpumask_test_cpu(cpu, &cpus_in_crash)) |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 44 | crash_save_cpu(regs, cpu); |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 45 | cpumask_set_cpu(cpu, &cpus_in_crash); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 46 | |
| 47 | while (!atomic_read(&kexec_ready_to_reboot)) |
| 48 | cpu_relax(); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 49 | |
| 50 | kexec_reboot(); |
| 51 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 52 | /* NOTREACHED */ |
| 53 | } |
| 54 | |
| 55 | static void crash_kexec_prepare_cpus(void) |
| 56 | { |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 57 | static int cpus_stopped; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 58 | unsigned int msecs; |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 59 | unsigned int ncpus; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 60 | |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 61 | if (cpus_stopped) |
| 62 | return; |
| 63 | |
| 64 | ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 65 | |
Marcin Nowakowski | c83c2ee | 2016-12-02 09:58:28 +0100 | [diff] [blame] | 66 | smp_call_function(crash_shutdown_secondary, NULL, 0); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 67 | smp_wmb(); |
| 68 | |
| 69 | /* |
| 70 | * The crash CPU sends an IPI and wait for other CPUs to |
| 71 | * respond. Delay of at least 10 seconds. |
| 72 | */ |
| 73 | pr_emerg("Sending IPI to other cpus...\n"); |
| 74 | msecs = 10000; |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 75 | while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 76 | cpu_relax(); |
| 77 | mdelay(1); |
| 78 | } |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 79 | |
| 80 | cpus_stopped = 1; |
| 81 | } |
| 82 | |
| 83 | /* Override the weak function in kernel/panic.c */ |
| 84 | void crash_smp_send_stop(void) |
| 85 | { |
| 86 | if (_crash_smp_send_stop) |
| 87 | _crash_smp_send_stop(); |
| 88 | |
| 89 | crash_kexec_prepare_cpus(); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #else /* !defined(CONFIG_SMP) */ |
| 93 | static void crash_kexec_prepare_cpus(void) {} |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 94 | #endif /* !defined(CONFIG_SMP) */ |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 95 | |
| 96 | void default_machine_crash_shutdown(struct pt_regs *regs) |
| 97 | { |
| 98 | local_irq_disable(); |
| 99 | crashing_cpu = smp_processor_id(); |
| 100 | crash_save_cpu(regs, crashing_cpu); |
| 101 | crash_kexec_prepare_cpus(); |
Rusty Russell | 8dd9289 | 2015-03-05 10:49:17 +1030 | [diff] [blame] | 102 | cpumask_set_cpu(crashing_cpu, &cpus_in_crash); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 103 | } |