reboot.c 14 KB

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