crash.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/memblock.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. /* This keeps a track of which one is crashing cpu. */
  14. static int crashing_cpu = -1;
  15. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  16. #ifdef CONFIG_SMP
  17. static void crash_shutdown_secondary(void *passed_regs)
  18. {
  19. struct pt_regs *regs = passed_regs;
  20. int cpu = smp_processor_id();
  21. /*
  22. * If we are passed registers, use those. Otherwise get the
  23. * regs from the last interrupt, which should be correct, as
  24. * we are in an interrupt. But if the regs are not there,
  25. * pull them from the top of the stack. They are probably
  26. * wrong, but we need something to keep from crashing again.
  27. */
  28. if (!regs)
  29. regs = get_irq_regs();
  30. if (!regs)
  31. regs = task_pt_regs(current);
  32. if (!cpu_online(cpu))
  33. return;
  34. /* We won't be sent IPIs any more. */
  35. set_cpu_online(cpu, false);
  36. local_irq_disable();
  37. if (!cpumask_test_cpu(cpu, &cpus_in_crash))
  38. crash_save_cpu(regs, cpu);
  39. cpumask_set_cpu(cpu, &cpus_in_crash);
  40. while (!atomic_read(&kexec_ready_to_reboot))
  41. cpu_relax();
  42. kexec_reboot();
  43. /* NOTREACHED */
  44. }
  45. static void crash_kexec_prepare_cpus(void)
  46. {
  47. static int cpus_stopped;
  48. unsigned int msecs;
  49. unsigned int ncpus;
  50. if (cpus_stopped)
  51. return;
  52. ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  53. smp_call_function(crash_shutdown_secondary, NULL, 0);
  54. smp_wmb();
  55. /*
  56. * The crash CPU sends an IPI and wait for other CPUs to
  57. * respond. Delay of at least 10 seconds.
  58. */
  59. pr_emerg("Sending IPI to other cpus...\n");
  60. msecs = 10000;
  61. while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
  62. cpu_relax();
  63. mdelay(1);
  64. }
  65. cpus_stopped = 1;
  66. }
  67. /* Override the weak function in kernel/panic.c */
  68. void crash_smp_send_stop(void)
  69. {
  70. if (_crash_smp_send_stop)
  71. _crash_smp_send_stop();
  72. crash_kexec_prepare_cpus();
  73. }
  74. #else /* !defined(CONFIG_SMP) */
  75. static void crash_kexec_prepare_cpus(void) {}
  76. #endif /* !defined(CONFIG_SMP) */
  77. void default_machine_crash_shutdown(struct pt_regs *regs)
  78. {
  79. local_irq_disable();
  80. crashing_cpu = smp_processor_id();
  81. crash_save_cpu(regs, crashing_cpu);
  82. crash_kexec_prepare_cpus();
  83. cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
  84. }