reboot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/reboot.c
  4. *
  5. * Copyright (C) 2013 Linus Torvalds
  6. */
  7. #define pr_fmt(fmt) "reboot: " fmt
  8. #include <linux/ctype.h>
  9. #include <linux/export.h>
  10. #include <linux/kexec.h>
  11. #include <linux/kmod.h>
  12. #include <linux/kmsg_dump.h>
  13. #include <linux/reboot.h>
  14. #include <linux/suspend.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/syscore_ops.h>
  17. #include <linux/uaccess.h>
  18. /*
  19. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  20. */
  21. int C_A_D = 1;
  22. struct pid *cad_pid;
  23. EXPORT_SYMBOL(cad_pid);
  24. #if defined(CONFIG_ARM)
  25. #define DEFAULT_REBOOT_MODE = REBOOT_HARD
  26. #else
  27. #define DEFAULT_REBOOT_MODE
  28. #endif
  29. enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
  30. EXPORT_SYMBOL_GPL(reboot_mode);
  31. enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
  32. EXPORT_SYMBOL_GPL(panic_reboot_mode);
  33. /*
  34. * This variable is used privately to keep track of whether or not
  35. * reboot_type is still set to its default value (i.e., reboot= hasn't
  36. * been set on the command line). This is needed so that we can
  37. * suppress DMI scanning for reboot quirks. Without it, it's
  38. * impossible to override a faulty reboot quirk without recompiling.
  39. */
  40. int reboot_default = 1;
  41. int reboot_cpu;
  42. enum reboot_type reboot_type = BOOT_ACPI;
  43. int reboot_force;
  44. /*
  45. * If set, this is used for preparing the system to power off.
  46. */
  47. void (*pm_power_off_prepare)(void);
  48. EXPORT_SYMBOL_GPL(pm_power_off_prepare);
  49. /**
  50. * emergency_restart - reboot the system
  51. *
  52. * Without shutting down any hardware or taking any locks
  53. * reboot the system. This is called when we know we are in
  54. * trouble so this is our best effort to reboot. This is
  55. * safe to call in interrupt context.
  56. */
  57. void emergency_restart(void)
  58. {
  59. kmsg_dump(KMSG_DUMP_EMERG);
  60. machine_emergency_restart();
  61. }
  62. EXPORT_SYMBOL_GPL(emergency_restart);
  63. void kernel_restart_prepare(char *cmd)
  64. {
  65. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  66. system_state = SYSTEM_RESTART;
  67. usermodehelper_disable();
  68. device_shutdown();
  69. }
  70. /**
  71. * register_reboot_notifier - Register function to be called at reboot time
  72. * @nb: Info about notifier function to be called
  73. *
  74. * Registers a function with the list of functions
  75. * to be called at reboot time.
  76. *
  77. * Currently always returns zero, as blocking_notifier_chain_register()
  78. * always returns zero.
  79. */
  80. int register_reboot_notifier(struct notifier_block *nb)
  81. {
  82. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  83. }
  84. EXPORT_SYMBOL(register_reboot_notifier);
  85. /**
  86. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  87. * @nb: Hook to be unregistered
  88. *
  89. * Unregisters a previously registered reboot
  90. * notifier function.
  91. *
  92. * Returns zero on success, or %-ENOENT on failure.
  93. */
  94. int unregister_reboot_notifier(struct notifier_block *nb)
  95. {
  96. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  97. }
  98. EXPORT_SYMBOL(unregister_reboot_notifier);
  99. static void devm_unregister_reboot_notifier(struct device *dev, void *res)
  100. {
  101. WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
  102. }
  103. int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
  104. {
  105. struct notifier_block **rcnb;
  106. int ret;
  107. rcnb = devres_alloc(devm_unregister_reboot_notifier,
  108. sizeof(*rcnb), GFP_KERNEL);
  109. if (!rcnb)
  110. return -ENOMEM;
  111. ret = register_reboot_notifier(nb);
  112. if (!ret) {
  113. *rcnb = nb;
  114. devres_add(dev, rcnb);
  115. } else {
  116. devres_free(rcnb);
  117. }
  118. return ret;
  119. }
  120. EXPORT_SYMBOL(devm_register_reboot_notifier);
  121. /*
  122. * Notifier list for kernel code which wants to be called
  123. * to restart the system.
  124. */
  125. static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
  126. /**
  127. * register_restart_handler - Register function to be called to reset
  128. * the system
  129. * @nb: Info about handler function to be called
  130. * @nb->priority: Handler priority. Handlers should follow the
  131. * following guidelines for setting priorities.
  132. * 0: Restart handler of last resort,
  133. * with limited restart capabilities
  134. * 128: Default restart handler; use if no other
  135. * restart handler is expected to be available,
  136. * and/or if restart functionality is
  137. * sufficient to restart the entire system
  138. * 255: Highest priority restart handler, will
  139. * preempt all other restart handlers
  140. *
  141. * Registers a function with code to be called to restart the
  142. * system.
  143. *
  144. * Registered functions will be called from machine_restart as last
  145. * step of the restart sequence (if the architecture specific
  146. * machine_restart function calls do_kernel_restart - see below
  147. * for details).
  148. * Registered functions are expected to restart the system immediately.
  149. * If more than one function is registered, the restart handler priority
  150. * selects which function will be called first.
  151. *
  152. * Restart handlers are expected to be registered from non-architecture
  153. * code, typically from drivers. A typical use case would be a system
  154. * where restart functionality is provided through a watchdog. Multiple
  155. * restart handlers may exist; for example, one restart handler might
  156. * restart the entire system, while another only restarts the CPU.
  157. * In such cases, the restart handler which only restarts part of the
  158. * hardware is expected to register with low priority to ensure that
  159. * it only runs if no other means to restart the system is available.
  160. *
  161. * Currently always returns zero, as atomic_notifier_chain_register()
  162. * always returns zero.
  163. */
  164. int register_restart_handler(struct notifier_block *nb)
  165. {
  166. return atomic_notifier_chain_register(&restart_handler_list, nb);
  167. }
  168. EXPORT_SYMBOL(register_restart_handler);
  169. /**
  170. * unregister_restart_handler - Unregister previously registered
  171. * restart handler
  172. * @nb: Hook to be unregistered
  173. *
  174. * Unregisters a previously registered restart handler function.
  175. *
  176. * Returns zero on success, or %-ENOENT on failure.
  177. */
  178. int unregister_restart_handler(struct notifier_block *nb)
  179. {
  180. return atomic_notifier_chain_unregister(&restart_handler_list, nb);
  181. }
  182. EXPORT_SYMBOL(unregister_restart_handler);
  183. /**
  184. * do_kernel_restart - Execute kernel restart handler call chain
  185. *
  186. * Calls functions registered with register_restart_handler.
  187. *
  188. * Expected to be called from machine_restart as last step of the restart
  189. * sequence.
  190. *
  191. * Restarts the system immediately if a restart handler function has been
  192. * registered. Otherwise does nothing.
  193. */
  194. void do_kernel_restart(char *cmd)
  195. {
  196. atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
  197. }
  198. void migrate_to_reboot_cpu(void)
  199. {
  200. /* The boot cpu is always logical cpu 0 */
  201. int cpu = reboot_cpu;
  202. cpu_hotplug_disable();
  203. /* Make certain the cpu I'm about to reboot on is online */
  204. if (!cpu_online(cpu))
  205. cpu = cpumask_first(cpu_online_mask);
  206. /* Prevent races with other tasks migrating this task */
  207. current->flags |= PF_NO_SETAFFINITY;
  208. /* Make certain I only run on the appropriate processor */
  209. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  210. }
  211. /**
  212. * kernel_restart - reboot the system
  213. * @cmd: pointer to buffer containing command to execute for restart
  214. * or %NULL
  215. *
  216. * Shutdown everything and perform a clean reboot.
  217. * This is not safe to call in interrupt context.
  218. */
  219. void kernel_restart(char *cmd)
  220. {
  221. kernel_restart_prepare(cmd);
  222. migrate_to_reboot_cpu();
  223. syscore_shutdown();
  224. if (!cmd)
  225. pr_emerg("Restarting system\n");
  226. else
  227. pr_emerg("Restarting system with command '%s'\n", cmd);
  228. kmsg_dump(KMSG_DUMP_SHUTDOWN);
  229. machine_restart(cmd);
  230. }
  231. EXPORT_SYMBOL_GPL(kernel_restart);
  232. static void kernel_shutdown_prepare(enum system_states state)
  233. {
  234. blocking_notifier_call_chain(&reboot_notifier_list,
  235. (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
  236. system_state = state;
  237. usermodehelper_disable();
  238. device_shutdown();
  239. }
  240. /**
  241. * kernel_halt - halt the system
  242. *
  243. * Shutdown everything and perform a clean system halt.
  244. */
  245. void kernel_halt(void)
  246. {
  247. kernel_shutdown_prepare(SYSTEM_HALT);
  248. migrate_to_reboot_cpu();
  249. syscore_shutdown();
  250. pr_emerg("System halted\n");
  251. kmsg_dump(KMSG_DUMP_SHUTDOWN);
  252. machine_halt();
  253. }
  254. EXPORT_SYMBOL_GPL(kernel_halt);
  255. /**
  256. * kernel_power_off - power_off the system
  257. *
  258. * Shutdown everything and perform a clean system power_off.
  259. */
  260. void kernel_power_off(void)
  261. {
  262. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  263. if (pm_power_off_prepare)
  264. pm_power_off_prepare();
  265. migrate_to_reboot_cpu();
  266. syscore_shutdown();
  267. pr_emerg("Power down\n");
  268. kmsg_dump(KMSG_DUMP_SHUTDOWN);
  269. machine_power_off();
  270. }
  271. EXPORT_SYMBOL_GPL(kernel_power_off);
  272. DEFINE_MUTEX(system_transition_mutex);
  273. /*
  274. * Reboot system call: for obvious reasons only root may call it,
  275. * and even root needs to set up some magic numbers in the registers
  276. * so that some mistake won't make this reboot the whole machine.
  277. * You can also set the meaning of the ctrl-alt-del-key here.
  278. *
  279. * reboot doesn't sync: do that yourself before calling this.
  280. */
  281. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  282. void __user *, arg)
  283. {
  284. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  285. char buffer[256];
  286. int ret = 0;
  287. /* We only trust the superuser with rebooting the system. */
  288. if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
  289. return -EPERM;
  290. /* For safety, we require "magic" arguments. */
  291. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  292. (magic2 != LINUX_REBOOT_MAGIC2 &&
  293. magic2 != LINUX_REBOOT_MAGIC2A &&
  294. magic2 != LINUX_REBOOT_MAGIC2B &&
  295. magic2 != LINUX_REBOOT_MAGIC2C))
  296. return -EINVAL;
  297. /*
  298. * If pid namespaces are enabled and the current task is in a child
  299. * pid_namespace, the command is handled by reboot_pid_ns() which will
  300. * call do_exit().
  301. */
  302. ret = reboot_pid_ns(pid_ns, cmd);
  303. if (ret)
  304. return ret;
  305. /* Instead of trying to make the power_off code look like
  306. * halt when pm_power_off is not set do it the easy way.
  307. */
  308. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  309. cmd = LINUX_REBOOT_CMD_HALT;
  310. mutex_lock(&system_transition_mutex);
  311. switch (cmd) {
  312. case LINUX_REBOOT_CMD_RESTART:
  313. kernel_restart(NULL);
  314. break;
  315. case LINUX_REBOOT_CMD_CAD_ON:
  316. C_A_D = 1;
  317. break;
  318. case LINUX_REBOOT_CMD_CAD_OFF:
  319. C_A_D = 0;
  320. break;
  321. case LINUX_REBOOT_CMD_HALT:
  322. kernel_halt();
  323. do_exit(0);
  324. panic("cannot halt");
  325. case LINUX_REBOOT_CMD_POWER_OFF:
  326. kernel_power_off();
  327. do_exit(0);
  328. break;
  329. case LINUX_REBOOT_CMD_RESTART2:
  330. ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
  331. if (ret < 0) {
  332. ret = -EFAULT;
  333. break;
  334. }
  335. buffer[sizeof(buffer) - 1] = '\0';
  336. kernel_restart(buffer);
  337. break;
  338. #ifdef CONFIG_KEXEC_CORE
  339. case LINUX_REBOOT_CMD_KEXEC:
  340. ret = kernel_kexec();
  341. break;
  342. #endif
  343. #ifdef CONFIG_HIBERNATION
  344. case LINUX_REBOOT_CMD_SW_SUSPEND:
  345. ret = hibernate();
  346. break;
  347. #endif
  348. default:
  349. ret = -EINVAL;
  350. break;
  351. }
  352. mutex_unlock(&system_transition_mutex);
  353. return ret;
  354. }
  355. static void deferred_cad(struct work_struct *dummy)
  356. {
  357. kernel_restart(NULL);
  358. }
  359. /*
  360. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  361. * As it's called within an interrupt, it may NOT sync: the only choice
  362. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  363. */
  364. void ctrl_alt_del(void)
  365. {
  366. static DECLARE_WORK(cad_work, deferred_cad);
  367. if (C_A_D)
  368. schedule_work(&cad_work);
  369. else
  370. kill_cad_pid(SIGINT, 1);
  371. }
  372. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  373. static const char reboot_cmd[] = "/sbin/reboot";
  374. static int run_cmd(const char *cmd)
  375. {
  376. char **argv;
  377. static char *envp[] = {
  378. "HOME=/",
  379. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  380. NULL
  381. };
  382. int ret;
  383. argv = argv_split(GFP_KERNEL, cmd, NULL);
  384. if (argv) {
  385. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  386. argv_free(argv);
  387. } else {
  388. ret = -ENOMEM;
  389. }
  390. return ret;
  391. }
  392. static int __orderly_reboot(void)
  393. {
  394. int ret;
  395. ret = run_cmd(reboot_cmd);
  396. if (ret) {
  397. pr_warn("Failed to start orderly reboot: forcing the issue\n");
  398. emergency_sync();
  399. kernel_restart(NULL);
  400. }
  401. return ret;
  402. }
  403. static int __orderly_poweroff(bool force)
  404. {
  405. int ret;
  406. ret = run_cmd(poweroff_cmd);
  407. if (ret && force) {
  408. pr_warn("Failed to start orderly shutdown: forcing the issue\n");
  409. /*
  410. * I guess this should try to kick off some daemon to sync and
  411. * poweroff asap. Or not even bother syncing if we're doing an
  412. * emergency shutdown?
  413. */
  414. emergency_sync();
  415. kernel_power_off();
  416. }
  417. return ret;
  418. }
  419. static bool poweroff_force;
  420. static void poweroff_work_func(struct work_struct *work)
  421. {
  422. __orderly_poweroff(poweroff_force);
  423. }
  424. static DECLARE_WORK(poweroff_work, poweroff_work_func);
  425. /**
  426. * orderly_poweroff - Trigger an orderly system poweroff
  427. * @force: force poweroff if command execution fails
  428. *
  429. * This may be called from any context to trigger a system shutdown.
  430. * If the orderly shutdown fails, it will force an immediate shutdown.
  431. */
  432. void orderly_poweroff(bool force)
  433. {
  434. if (force) /* do not override the pending "true" */
  435. poweroff_force = true;
  436. schedule_work(&poweroff_work);
  437. }
  438. EXPORT_SYMBOL_GPL(orderly_poweroff);
  439. static void reboot_work_func(struct work_struct *work)
  440. {
  441. __orderly_reboot();
  442. }
  443. static DECLARE_WORK(reboot_work, reboot_work_func);
  444. /**
  445. * orderly_reboot - Trigger an orderly system reboot
  446. *
  447. * This may be called from any context to trigger a system reboot.
  448. * If the orderly reboot fails, it will force an immediate reboot.
  449. */
  450. void orderly_reboot(void)
  451. {
  452. schedule_work(&reboot_work);
  453. }
  454. EXPORT_SYMBOL_GPL(orderly_reboot);
  455. static int __init reboot_setup(char *str)
  456. {
  457. for (;;) {
  458. enum reboot_mode *mode;
  459. /*
  460. * Having anything passed on the command line via
  461. * reboot= will cause us to disable DMI checking
  462. * below.
  463. */
  464. reboot_default = 0;
  465. if (!strncmp(str, "panic_", 6)) {
  466. mode = &panic_reboot_mode;
  467. str += 6;
  468. } else {
  469. mode = &reboot_mode;
  470. }
  471. switch (*str) {
  472. case 'w':
  473. *mode = REBOOT_WARM;
  474. break;
  475. case 'c':
  476. *mode = REBOOT_COLD;
  477. break;
  478. case 'h':
  479. *mode = REBOOT_HARD;
  480. break;
  481. case 's':
  482. if (isdigit(*(str+1)))
  483. reboot_cpu = simple_strtoul(str+1, NULL, 0);
  484. else if (str[1] == 'm' && str[2] == 'p' &&
  485. isdigit(*(str+3)))
  486. reboot_cpu = simple_strtoul(str+3, NULL, 0);
  487. else
  488. *mode = REBOOT_SOFT;
  489. if (reboot_cpu >= num_possible_cpus()) {
  490. pr_err("Ignoring the CPU number in reboot= option. "
  491. "CPU %d exceeds possible cpu number %d\n",
  492. reboot_cpu, num_possible_cpus());
  493. reboot_cpu = 0;
  494. break;
  495. }
  496. break;
  497. case 'g':
  498. *mode = REBOOT_GPIO;
  499. break;
  500. case 'b':
  501. case 'a':
  502. case 'k':
  503. case 't':
  504. case 'e':
  505. case 'p':
  506. reboot_type = *str;
  507. break;
  508. case 'f':
  509. reboot_force = 1;
  510. break;
  511. }
  512. str = strchr(str, ',');
  513. if (str)
  514. str++;
  515. else
  516. break;
  517. }
  518. return 1;
  519. }
  520. __setup("reboot=", reboot_setup);