Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/kernel.h>
3#include <linux/smp.h>
4#include <linux/reboot.h>
5#include <linux/kexec.h>
6#include <linux/bootmem.h>
7#include <linux/crash_dump.h>
8#include <linux/delay.h>
9#include <linux/irq.h>
10#include <linux/types.h>
11#include <linux/sched.h>
12#include <linux/sched/task_stack.h>
13
14/* This keeps a track of which one is crashing cpu. */
15static int crashing_cpu = -1;
16static cpumask_t cpus_in_crash = CPU_MASK_NONE;
17
18#ifdef CONFIG_SMP
19static void crash_shutdown_secondary(void *passed_regs)
20{
21 struct pt_regs *regs = passed_regs;
22 int cpu = smp_processor_id();
23
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);
35
36 if (!cpu_online(cpu))
37 return;
38
39 local_irq_disable();
40 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
41 crash_save_cpu(regs, cpu);
42 cpumask_set_cpu(cpu, &cpus_in_crash);
43
44 while (!atomic_read(&kexec_ready_to_reboot))
45 cpu_relax();
46 relocated_kexec_smp_wait(NULL);
47 /* NOTREACHED */
48}
49
50static void crash_kexec_prepare_cpus(void)
51{
52 static int cpus_stopped;
53 unsigned int msecs;
54 unsigned int ncpus;
55
56 if (cpus_stopped)
57 return;
58
59 ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
60
61 smp_call_function(crash_shutdown_secondary, NULL, 0);
62 smp_wmb();
63
64 /*
65 * The crash CPU sends an IPI and wait for other CPUs to
66 * respond. Delay of at least 10 seconds.
67 */
68 pr_emerg("Sending IPI to other cpus...\n");
69 msecs = 10000;
70 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
71 cpu_relax();
72 mdelay(1);
73 }
74
75 cpus_stopped = 1;
76}
77
78/* Override the weak function in kernel/panic.c */
79void crash_smp_send_stop(void)
80{
81 if (_crash_smp_send_stop)
82 _crash_smp_send_stop();
83
84 crash_kexec_prepare_cpus();
85}
86
87#else /* !defined(CONFIG_SMP) */
88static void crash_kexec_prepare_cpus(void) {}
89#endif /* !defined(CONFIG_SMP) */
90
91void default_machine_crash_shutdown(struct pt_regs *regs)
92{
93 local_irq_disable();
94 crashing_cpu = smp_processor_id();
95 crash_save_cpu(regs, crashing_cpu);
96 crash_kexec_prepare_cpus();
97 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
98}
1#include <linux/kernel.h>
2#include <linux/smp.h>
3#include <linux/reboot.h>
4#include <linux/kexec.h>
5#include <linux/bootmem.h>
6#include <linux/crash_dump.h>
7#include <linux/delay.h>
8#include <linux/irq.h>
9#include <linux/types.h>
10#include <linux/sched.h>
11
12/* This keeps a track of which one is crashing cpu. */
13static int crashing_cpu = -1;
14static cpumask_t cpus_in_crash = CPU_MASK_NONE;
15
16#ifdef CONFIG_SMP
17static void crash_shutdown_secondary(void *ignore)
18{
19 struct pt_regs *regs;
20 int cpu = smp_processor_id();
21
22 regs = task_pt_regs(current);
23
24 if (!cpu_online(cpu))
25 return;
26
27 local_irq_disable();
28 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
29 crash_save_cpu(regs, cpu);
30 cpumask_set_cpu(cpu, &cpus_in_crash);
31
32 while (!atomic_read(&kexec_ready_to_reboot))
33 cpu_relax();
34 relocated_kexec_smp_wait(NULL);
35 /* NOTREACHED */
36}
37
38static void crash_kexec_prepare_cpus(void)
39{
40 unsigned int msecs;
41
42 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
43
44 dump_send_ipi(crash_shutdown_secondary);
45 smp_wmb();
46
47 /*
48 * The crash CPU sends an IPI and wait for other CPUs to
49 * respond. Delay of at least 10 seconds.
50 */
51 pr_emerg("Sending IPI to other cpus...\n");
52 msecs = 10000;
53 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
54 cpu_relax();
55 mdelay(1);
56 }
57}
58
59#else /* !defined(CONFIG_SMP) */
60static void crash_kexec_prepare_cpus(void) {}
61#endif /* !defined(CONFIG_SMP) */
62
63void default_machine_crash_shutdown(struct pt_regs *regs)
64{
65 local_irq_disable();
66 crashing_cpu = smp_processor_id();
67 crash_save_cpu(regs, crashing_cpu);
68 crash_kexec_prepare_cpus();
69 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
70}