reboot.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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/atomic.h>
  9. #include <linux/ctype.h>
  10. #include <linux/export.h>
  11. #include <linux/kexec.h>
  12. #include <linux/kmod.h>
  13. #include <linux/kmsg_dump.h>
  14. #include <linux/reboot.h>
  15. #include <linux/suspend.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/uaccess.h>
  19. /*
  20. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  21. */
  22. static int C_A_D = 1;
  23. struct pid *cad_pid;
  24. EXPORT_SYMBOL(cad_pid);
  25. #if defined(CONFIG_ARM)
  26. #define DEFAULT_REBOOT_MODE = REBOOT_HARD
  27. #else
  28. #define DEFAULT_REBOOT_MODE
  29. #endif
  30. enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
  31. EXPORT_SYMBOL_GPL(reboot_mode);
  32. enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
  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. struct sys_off_handler {
  45. struct notifier_block nb;
  46. int (*sys_off_cb)(struct sys_off_data *data);
  47. void *cb_data;
  48. enum sys_off_mode mode;
  49. bool blocking;
  50. void *list;
  51. struct device *dev;
  52. };
  53. /*
  54. * This variable is used to indicate if a halt was initiated instead of a
  55. * reboot when the reboot call was invoked with LINUX_REBOOT_CMD_POWER_OFF, but
  56. * the system cannot be powered off. This allowes kernel_halt() to notify users
  57. * of that.
  58. */
  59. static bool poweroff_fallback_to_halt;
  60. /*
  61. * Temporary stub that prevents linkage failure while we're in process
  62. * of removing all uses of legacy pm_power_off() around the kernel.
  63. */
  64. void __weak (*pm_power_off)(void);
  65. /**
  66. * emergency_restart - reboot the system
  67. *
  68. * Without shutting down any hardware or taking any locks
  69. * reboot the system. This is called when we know we are in
  70. * trouble so this is our best effort to reboot. This is
  71. * safe to call in interrupt context.
  72. */
  73. void emergency_restart(void)
  74. {
  75. kmsg_dump(KMSG_DUMP_EMERG);
  76. system_state = SYSTEM_RESTART;
  77. machine_emergency_restart();
  78. }
  79. EXPORT_SYMBOL_GPL(emergency_restart);
  80. void kernel_restart_prepare(char *cmd)
  81. {
  82. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  83. system_state = SYSTEM_RESTART;
  84. usermodehelper_disable();
  85. device_shutdown();
  86. }
  87. /**
  88. * register_reboot_notifier - Register function to be called at reboot time
  89. * @nb: Info about notifier function to be called
  90. *
  91. * Registers a function with the list of functions
  92. * to be called at reboot time.
  93. *
  94. * Currently always returns zero, as blocking_notifier_chain_register()
  95. * always returns zero.
  96. */
  97. int register_reboot_notifier(struct notifier_block *nb)
  98. {
  99. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  100. }
  101. EXPORT_SYMBOL(register_reboot_notifier);
  102. /**
  103. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  104. * @nb: Hook to be unregistered
  105. *
  106. * Unregisters a previously registered reboot
  107. * notifier function.
  108. *
  109. * Returns zero on success, or %-ENOENT on failure.
  110. */
  111. int unregister_reboot_notifier(struct notifier_block *nb)
  112. {
  113. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  114. }
  115. EXPORT_SYMBOL(unregister_reboot_notifier);
  116. static void devm_unregister_reboot_notifier(struct device *dev, void *res)
  117. {
  118. WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
  119. }
  120. int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
  121. {
  122. struct notifier_block **rcnb;
  123. int ret;
  124. rcnb = devres_alloc(devm_unregister_reboot_notifier,
  125. sizeof(*rcnb), GFP_KERNEL);
  126. if (!rcnb)
  127. return -ENOMEM;
  128. ret = register_reboot_notifier(nb);
  129. if (!ret) {
  130. *rcnb = nb;
  131. devres_add(dev, rcnb);
  132. } else {
  133. devres_free(rcnb);
  134. }
  135. return ret;
  136. }
  137. EXPORT_SYMBOL(devm_register_reboot_notifier);
  138. /*
  139. * Notifier list for kernel code which wants to be called
  140. * to restart the system.
  141. */
  142. static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
  143. /**
  144. * register_restart_handler - Register function to be called to reset
  145. * the system
  146. * @nb: Info about handler function to be called
  147. * @nb->priority: Handler priority. Handlers should follow the
  148. * following guidelines for setting priorities.
  149. * 0: Restart handler of last resort,
  150. * with limited restart capabilities
  151. * 128: Default restart handler; use if no other
  152. * restart handler is expected to be available,
  153. * and/or if restart functionality is
  154. * sufficient to restart the entire system
  155. * 255: Highest priority restart handler, will
  156. * preempt all other restart handlers
  157. *
  158. * Registers a function with code to be called to restart the
  159. * system.
  160. *
  161. * Registered functions will be called from machine_restart as last
  162. * step of the restart sequence (if the architecture specific
  163. * machine_restart function calls do_kernel_restart - see below
  164. * for details).
  165. * Registered functions are expected to restart the system immediately.
  166. * If more than one function is registered, the restart handler priority
  167. * selects which function will be called first.
  168. *
  169. * Restart handlers are expected to be registered from non-architecture
  170. * code, typically from drivers. A typical use case would be a system
  171. * where restart functionality is provided through a watchdog. Multiple
  172. * restart handlers may exist; for example, one restart handler might
  173. * restart the entire system, while another only restarts the CPU.
  174. * In such cases, the restart handler which only restarts part of the
  175. * hardware is expected to register with low priority to ensure that
  176. * it only runs if no other means to restart the system is available.
  177. *
  178. * Currently always returns zero, as atomic_notifier_chain_register()
  179. * always returns zero.
  180. */
  181. int register_restart_handler(struct notifier_block *nb)
  182. {
  183. return atomic_notifier_chain_register(&restart_handler_list, nb);
  184. }
  185. EXPORT_SYMBOL(register_restart_handler);
  186. /**
  187. * unregister_restart_handler - Unregister previously registered
  188. * restart handler
  189. * @nb: Hook to be unregistered
  190. *
  191. * Unregisters a previously registered restart handler function.
  192. *
  193. * Returns zero on success, or %-ENOENT on failure.
  194. */
  195. int unregister_restart_handler(struct notifier_block *nb)
  196. {
  197. return atomic_notifier_chain_unregister(&restart_handler_list, nb);
  198. }
  199. EXPORT_SYMBOL(unregister_restart_handler);
  200. /**
  201. * do_kernel_restart - Execute kernel restart handler call chain
  202. *
  203. * Calls functions registered with register_restart_handler.
  204. *
  205. * Expected to be called from machine_restart as last step of the restart
  206. * sequence.
  207. *
  208. * Restarts the system immediately if a restart handler function has been
  209. * registered. Otherwise does nothing.
  210. */
  211. void do_kernel_restart(char *cmd)
  212. {
  213. atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
  214. }
  215. void migrate_to_reboot_cpu(void)
  216. {
  217. /* The boot cpu is always logical cpu 0 */
  218. int cpu = reboot_cpu;
  219. cpu_hotplug_disable();
  220. /* Make certain the cpu I'm about to reboot on is online */
  221. if (!cpu_online(cpu))
  222. cpu = cpumask_first(cpu_online_mask);
  223. /* Prevent races with other tasks migrating this task */
  224. current->flags |= PF_NO_SETAFFINITY;
  225. /* Make certain I only run on the appropriate processor */
  226. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  227. }
  228. /*
  229. * Notifier list for kernel code which wants to be called
  230. * to prepare system for restart.
  231. */
  232. static BLOCKING_NOTIFIER_HEAD(restart_prep_handler_list);
  233. static void do_kernel_restart_prepare(void)
  234. {
  235. blocking_notifier_call_chain(&restart_prep_handler_list, 0, NULL);
  236. }
  237. /**
  238. * kernel_restart - reboot the system
  239. * @cmd: pointer to buffer containing command to execute for restart
  240. * or %NULL
  241. *
  242. * Shutdown everything and perform a clean reboot.
  243. * This is not safe to call in interrupt context.
  244. */
  245. void kernel_restart(char *cmd)
  246. {
  247. kernel_restart_prepare(cmd);
  248. do_kernel_restart_prepare();
  249. migrate_to_reboot_cpu();
  250. syscore_shutdown();
  251. if (!cmd)
  252. pr_emerg("Restarting system\n");
  253. else
  254. pr_emerg("Restarting system with command '%s'\n", cmd);
  255. kmsg_dump(KMSG_DUMP_SHUTDOWN);
  256. machine_restart(cmd);
  257. }
  258. EXPORT_SYMBOL_GPL(kernel_restart);
  259. static void kernel_shutdown_prepare(enum system_states state)
  260. {
  261. blocking_notifier_call_chain(&reboot_notifier_list,
  262. (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
  263. system_state = state;
  264. usermodehelper_disable();
  265. device_shutdown();
  266. }
  267. /**
  268. * kernel_halt - halt the system
  269. *
  270. * Shutdown everything and perform a clean system halt.
  271. */
  272. void kernel_halt(void)
  273. {
  274. kernel_shutdown_prepare(SYSTEM_HALT);
  275. migrate_to_reboot_cpu();
  276. syscore_shutdown();
  277. if (poweroff_fallback_to_halt)
  278. pr_emerg("Power off not available: System halted instead\n");
  279. else
  280. pr_emerg("System halted\n");
  281. kmsg_dump(KMSG_DUMP_SHUTDOWN);
  282. machine_halt();
  283. }
  284. EXPORT_SYMBOL_GPL(kernel_halt);
  285. /*
  286. * Notifier list for kernel code which wants to be called
  287. * to prepare system for power off.
  288. */
  289. static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
  290. /*
  291. * Notifier list for kernel code which wants to be called
  292. * to power off system.
  293. */
  294. static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
  295. static int sys_off_notify(struct notifier_block *nb,
  296. unsigned long mode, void *cmd)
  297. {
  298. struct sys_off_handler *handler;
  299. struct sys_off_data data = {};
  300. handler = container_of(nb, struct sys_off_handler, nb);
  301. data.cb_data = handler->cb_data;
  302. data.mode = mode;
  303. data.cmd = cmd;
  304. data.dev = handler->dev;
  305. return handler->sys_off_cb(&data);
  306. }
  307. static struct sys_off_handler platform_sys_off_handler;
  308. static struct sys_off_handler *alloc_sys_off_handler(int priority)
  309. {
  310. struct sys_off_handler *handler;
  311. gfp_t flags;
  312. /*
  313. * Platforms like m68k can't allocate sys_off handler dynamically
  314. * at the early boot time because memory allocator isn't available yet.
  315. */
  316. if (priority == SYS_OFF_PRIO_PLATFORM) {
  317. handler = &platform_sys_off_handler;
  318. if (handler->cb_data)
  319. return ERR_PTR(-EBUSY);
  320. } else {
  321. if (system_state > SYSTEM_RUNNING)
  322. flags = GFP_ATOMIC;
  323. else
  324. flags = GFP_KERNEL;
  325. handler = kzalloc(sizeof(*handler), flags);
  326. if (!handler)
  327. return ERR_PTR(-ENOMEM);
  328. }
  329. return handler;
  330. }
  331. static void free_sys_off_handler(struct sys_off_handler *handler)
  332. {
  333. if (handler == &platform_sys_off_handler)
  334. memset(handler, 0, sizeof(*handler));
  335. else
  336. kfree(handler);
  337. }
  338. /**
  339. * register_sys_off_handler - Register sys-off handler
  340. * @mode: Sys-off mode
  341. * @priority: Handler priority
  342. * @callback: Callback function
  343. * @cb_data: Callback argument
  344. *
  345. * Registers system power-off or restart handler that will be invoked
  346. * at the step corresponding to the given sys-off mode. Handler's callback
  347. * should return NOTIFY_DONE to permit execution of the next handler in
  348. * the call chain or NOTIFY_STOP to break the chain (in error case for
  349. * example).
  350. *
  351. * Multiple handlers can be registered at the default priority level.
  352. *
  353. * Only one handler can be registered at the non-default priority level,
  354. * otherwise ERR_PTR(-EBUSY) is returned.
  355. *
  356. * Returns a new instance of struct sys_off_handler on success, or
  357. * an ERR_PTR()-encoded error code otherwise.
  358. */
  359. struct sys_off_handler *
  360. register_sys_off_handler(enum sys_off_mode mode,
  361. int priority,
  362. int (*callback)(struct sys_off_data *data),
  363. void *cb_data)
  364. {
  365. struct sys_off_handler *handler;
  366. int err;
  367. handler = alloc_sys_off_handler(priority);
  368. if (IS_ERR(handler))
  369. return handler;
  370. switch (mode) {
  371. case SYS_OFF_MODE_POWER_OFF_PREPARE:
  372. handler->list = &power_off_prep_handler_list;
  373. handler->blocking = true;
  374. break;
  375. case SYS_OFF_MODE_POWER_OFF:
  376. handler->list = &power_off_handler_list;
  377. break;
  378. case SYS_OFF_MODE_RESTART_PREPARE:
  379. handler->list = &restart_prep_handler_list;
  380. handler->blocking = true;
  381. break;
  382. case SYS_OFF_MODE_RESTART:
  383. handler->list = &restart_handler_list;
  384. break;
  385. default:
  386. free_sys_off_handler(handler);
  387. return ERR_PTR(-EINVAL);
  388. }
  389. handler->nb.notifier_call = sys_off_notify;
  390. handler->nb.priority = priority;
  391. handler->sys_off_cb = callback;
  392. handler->cb_data = cb_data;
  393. handler->mode = mode;
  394. if (handler->blocking) {
  395. if (priority == SYS_OFF_PRIO_DEFAULT)
  396. err = blocking_notifier_chain_register(handler->list,
  397. &handler->nb);
  398. else
  399. err = blocking_notifier_chain_register_unique_prio(handler->list,
  400. &handler->nb);
  401. } else {
  402. if (priority == SYS_OFF_PRIO_DEFAULT)
  403. err = atomic_notifier_chain_register(handler->list,
  404. &handler->nb);
  405. else
  406. err = atomic_notifier_chain_register_unique_prio(handler->list,
  407. &handler->nb);
  408. }
  409. if (err) {
  410. free_sys_off_handler(handler);
  411. return ERR_PTR(err);
  412. }
  413. return handler;
  414. }
  415. EXPORT_SYMBOL_GPL(register_sys_off_handler);
  416. /**
  417. * unregister_sys_off_handler - Unregister sys-off handler
  418. * @handler: Sys-off handler
  419. *
  420. * Unregisters given sys-off handler.
  421. */
  422. void unregister_sys_off_handler(struct sys_off_handler *handler)
  423. {
  424. int err;
  425. if (IS_ERR_OR_NULL(handler))
  426. return;
  427. if (handler->blocking)
  428. err = blocking_notifier_chain_unregister(handler->list,
  429. &handler->nb);
  430. else
  431. err = atomic_notifier_chain_unregister(handler->list,
  432. &handler->nb);
  433. /* sanity check, shall never happen */
  434. WARN_ON(err);
  435. free_sys_off_handler(handler);
  436. }
  437. EXPORT_SYMBOL_GPL(unregister_sys_off_handler);
  438. static void devm_unregister_sys_off_handler(void *data)
  439. {
  440. struct sys_off_handler *handler = data;
  441. unregister_sys_off_handler(handler);
  442. }
  443. /**
  444. * devm_register_sys_off_handler - Register sys-off handler
  445. * @dev: Device that registers handler
  446. * @mode: Sys-off mode
  447. * @priority: Handler priority
  448. * @callback: Callback function
  449. * @cb_data: Callback argument
  450. *
  451. * Registers resource-managed sys-off handler.
  452. *
  453. * Returns zero on success, or error code on failure.
  454. */
  455. int devm_register_sys_off_handler(struct device *dev,
  456. enum sys_off_mode mode,
  457. int priority,
  458. int (*callback)(struct sys_off_data *data),
  459. void *cb_data)
  460. {
  461. struct sys_off_handler *handler;
  462. handler = register_sys_off_handler(mode, priority, callback, cb_data);
  463. if (IS_ERR(handler))
  464. return PTR_ERR(handler);
  465. handler->dev = dev;
  466. return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler,
  467. handler);
  468. }
  469. EXPORT_SYMBOL_GPL(devm_register_sys_off_handler);
  470. /**
  471. * devm_register_power_off_handler - Register power-off handler
  472. * @dev: Device that registers callback
  473. * @callback: Callback function
  474. * @cb_data: Callback's argument
  475. *
  476. * Registers resource-managed sys-off handler with a default priority
  477. * and using power-off mode.
  478. *
  479. * Returns zero on success, or error code on failure.
  480. */
  481. int devm_register_power_off_handler(struct device *dev,
  482. int (*callback)(struct sys_off_data *data),
  483. void *cb_data)
  484. {
  485. return devm_register_sys_off_handler(dev,
  486. SYS_OFF_MODE_POWER_OFF,
  487. SYS_OFF_PRIO_DEFAULT,
  488. callback, cb_data);
  489. }
  490. EXPORT_SYMBOL_GPL(devm_register_power_off_handler);
  491. /**
  492. * devm_register_restart_handler - Register restart handler
  493. * @dev: Device that registers callback
  494. * @callback: Callback function
  495. * @cb_data: Callback's argument
  496. *
  497. * Registers resource-managed sys-off handler with a default priority
  498. * and using restart mode.
  499. *
  500. * Returns zero on success, or error code on failure.
  501. */
  502. int devm_register_restart_handler(struct device *dev,
  503. int (*callback)(struct sys_off_data *data),
  504. void *cb_data)
  505. {
  506. return devm_register_sys_off_handler(dev,
  507. SYS_OFF_MODE_RESTART,
  508. SYS_OFF_PRIO_DEFAULT,
  509. callback, cb_data);
  510. }
  511. EXPORT_SYMBOL_GPL(devm_register_restart_handler);
  512. static struct sys_off_handler *platform_power_off_handler;
  513. static int platform_power_off_notify(struct sys_off_data *data)
  514. {
  515. void (*platform_power_power_off_cb)(void) = data->cb_data;
  516. platform_power_power_off_cb();
  517. return NOTIFY_DONE;
  518. }
  519. /**
  520. * register_platform_power_off - Register platform-level power-off callback
  521. * @power_off: Power-off callback
  522. *
  523. * Registers power-off callback that will be called as last step
  524. * of the power-off sequence. This callback is expected to be invoked
  525. * for the last resort. Only one platform power-off callback is allowed
  526. * to be registered at a time.
  527. *
  528. * Returns zero on success, or error code on failure.
  529. */
  530. int register_platform_power_off(void (*power_off)(void))
  531. {
  532. struct sys_off_handler *handler;
  533. handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
  534. SYS_OFF_PRIO_PLATFORM,
  535. platform_power_off_notify,
  536. power_off);
  537. if (IS_ERR(handler))
  538. return PTR_ERR(handler);
  539. platform_power_off_handler = handler;
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(register_platform_power_off);
  543. /**
  544. * unregister_platform_power_off - Unregister platform-level power-off callback
  545. * @power_off: Power-off callback
  546. *
  547. * Unregisters previously registered platform power-off callback.
  548. */
  549. void unregister_platform_power_off(void (*power_off)(void))
  550. {
  551. if (platform_power_off_handler &&
  552. platform_power_off_handler->cb_data == power_off) {
  553. unregister_sys_off_handler(platform_power_off_handler);
  554. platform_power_off_handler = NULL;
  555. }
  556. }
  557. EXPORT_SYMBOL_GPL(unregister_platform_power_off);
  558. static int legacy_pm_power_off(struct sys_off_data *data)
  559. {
  560. if (pm_power_off)
  561. pm_power_off();
  562. return NOTIFY_DONE;
  563. }
  564. static void do_kernel_power_off_prepare(void)
  565. {
  566. blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL);
  567. }
  568. /**
  569. * do_kernel_power_off - Execute kernel power-off handler call chain
  570. *
  571. * Expected to be called as last step of the power-off sequence.
  572. *
  573. * Powers off the system immediately if a power-off handler function has
  574. * been registered. Otherwise does nothing.
  575. */
  576. void do_kernel_power_off(void)
  577. {
  578. struct sys_off_handler *sys_off = NULL;
  579. /*
  580. * Register sys-off handlers for legacy PM callback. This allows
  581. * legacy PM callbacks temporary co-exist with the new sys-off API.
  582. *
  583. * TODO: Remove legacy handlers once all legacy PM users will be
  584. * switched to the sys-off based APIs.
  585. */
  586. if (pm_power_off)
  587. sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
  588. SYS_OFF_PRIO_DEFAULT,
  589. legacy_pm_power_off, NULL);
  590. atomic_notifier_call_chain(&power_off_handler_list, 0, NULL);
  591. unregister_sys_off_handler(sys_off);
  592. }
  593. /**
  594. * kernel_can_power_off - check whether system can be powered off
  595. *
  596. * Returns true if power-off handler is registered and system can be
  597. * powered off, false otherwise.
  598. */
  599. bool kernel_can_power_off(void)
  600. {
  601. return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
  602. pm_power_off;
  603. }
  604. EXPORT_SYMBOL_GPL(kernel_can_power_off);
  605. /**
  606. * kernel_power_off - power_off the system
  607. *
  608. * Shutdown everything and perform a clean system power_off.
  609. */
  610. void kernel_power_off(void)
  611. {
  612. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  613. do_kernel_power_off_prepare();
  614. migrate_to_reboot_cpu();
  615. syscore_shutdown();
  616. pr_emerg("Power down\n");
  617. kmsg_dump(KMSG_DUMP_SHUTDOWN);
  618. machine_power_off();
  619. }
  620. EXPORT_SYMBOL_GPL(kernel_power_off);
  621. DEFINE_MUTEX(system_transition_mutex);
  622. /*
  623. * Reboot system call: for obvious reasons only root may call it,
  624. * and even root needs to set up some magic numbers in the registers
  625. * so that some mistake won't make this reboot the whole machine.
  626. * You can also set the meaning of the ctrl-alt-del-key here.
  627. *
  628. * reboot doesn't sync: do that yourself before calling this.
  629. */
  630. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  631. void __user *, arg)
  632. {
  633. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  634. char buffer[256];
  635. int ret = 0;
  636. /* We only trust the superuser with rebooting the system. */
  637. if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
  638. return -EPERM;
  639. /* For safety, we require "magic" arguments. */
  640. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  641. (magic2 != LINUX_REBOOT_MAGIC2 &&
  642. magic2 != LINUX_REBOOT_MAGIC2A &&
  643. magic2 != LINUX_REBOOT_MAGIC2B &&
  644. magic2 != LINUX_REBOOT_MAGIC2C))
  645. return -EINVAL;
  646. /*
  647. * If pid namespaces are enabled and the current task is in a child
  648. * pid_namespace, the command is handled by reboot_pid_ns() which will
  649. * call do_exit().
  650. */
  651. ret = reboot_pid_ns(pid_ns, cmd);
  652. if (ret)
  653. return ret;
  654. /* Instead of trying to make the power_off code look like
  655. * halt when pm_power_off is not set do it the easy way.
  656. */
  657. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off()) {
  658. poweroff_fallback_to_halt = true;
  659. cmd = LINUX_REBOOT_CMD_HALT;
  660. }
  661. mutex_lock(&system_transition_mutex);
  662. switch (cmd) {
  663. case LINUX_REBOOT_CMD_RESTART:
  664. kernel_restart(NULL);
  665. break;
  666. case LINUX_REBOOT_CMD_CAD_ON:
  667. C_A_D = 1;
  668. break;
  669. case LINUX_REBOOT_CMD_CAD_OFF:
  670. C_A_D = 0;
  671. break;
  672. case LINUX_REBOOT_CMD_HALT:
  673. kernel_halt();
  674. do_exit(0);
  675. case LINUX_REBOOT_CMD_POWER_OFF:
  676. kernel_power_off();
  677. do_exit(0);
  678. break;
  679. case LINUX_REBOOT_CMD_RESTART2:
  680. ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
  681. if (ret < 0) {
  682. ret = -EFAULT;
  683. break;
  684. }
  685. buffer[sizeof(buffer) - 1] = '\0';
  686. kernel_restart(buffer);
  687. break;
  688. #ifdef CONFIG_KEXEC_CORE
  689. case LINUX_REBOOT_CMD_KEXEC:
  690. ret = kernel_kexec();
  691. break;
  692. #endif
  693. #ifdef CONFIG_HIBERNATION
  694. case LINUX_REBOOT_CMD_SW_SUSPEND:
  695. ret = hibernate();
  696. break;
  697. #endif
  698. default:
  699. ret = -EINVAL;
  700. break;
  701. }
  702. mutex_unlock(&system_transition_mutex);
  703. return ret;
  704. }
  705. static void deferred_cad(struct work_struct *dummy)
  706. {
  707. kernel_restart(NULL);
  708. }
  709. /*
  710. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  711. * As it's called within an interrupt, it may NOT sync: the only choice
  712. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  713. */
  714. void ctrl_alt_del(void)
  715. {
  716. static DECLARE_WORK(cad_work, deferred_cad);
  717. if (C_A_D)
  718. schedule_work(&cad_work);
  719. else
  720. kill_cad_pid(SIGINT, 1);
  721. }
  722. #define POWEROFF_CMD_PATH_LEN 256
  723. static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  724. static const char reboot_cmd[] = "/sbin/reboot";
  725. static int run_cmd(const char *cmd)
  726. {
  727. char **argv;
  728. static char *envp[] = {
  729. "HOME=/",
  730. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  731. NULL
  732. };
  733. int ret;
  734. argv = argv_split(GFP_KERNEL, cmd, NULL);
  735. if (argv) {
  736. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  737. argv_free(argv);
  738. } else {
  739. ret = -ENOMEM;
  740. }
  741. return ret;
  742. }
  743. static int __orderly_reboot(void)
  744. {
  745. int ret;
  746. ret = run_cmd(reboot_cmd);
  747. if (ret) {
  748. pr_warn("Failed to start orderly reboot: forcing the issue\n");
  749. emergency_sync();
  750. kernel_restart(NULL);
  751. }
  752. return ret;
  753. }
  754. static int __orderly_poweroff(bool force)
  755. {
  756. int ret;
  757. ret = run_cmd(poweroff_cmd);
  758. if (ret && force) {
  759. pr_warn("Failed to start orderly shutdown: forcing the issue\n");
  760. /*
  761. * I guess this should try to kick off some daemon to sync and
  762. * poweroff asap. Or not even bother syncing if we're doing an
  763. * emergency shutdown?
  764. */
  765. emergency_sync();
  766. kernel_power_off();
  767. }
  768. return ret;
  769. }
  770. static bool poweroff_force;
  771. static void poweroff_work_func(struct work_struct *work)
  772. {
  773. __orderly_poweroff(poweroff_force);
  774. }
  775. static DECLARE_WORK(poweroff_work, poweroff_work_func);
  776. /**
  777. * orderly_poweroff - Trigger an orderly system poweroff
  778. * @force: force poweroff if command execution fails
  779. *
  780. * This may be called from any context to trigger a system shutdown.
  781. * If the orderly shutdown fails, it will force an immediate shutdown.
  782. */
  783. void orderly_poweroff(bool force)
  784. {
  785. if (force) /* do not override the pending "true" */
  786. poweroff_force = true;
  787. schedule_work(&poweroff_work);
  788. }
  789. EXPORT_SYMBOL_GPL(orderly_poweroff);
  790. static void reboot_work_func(struct work_struct *work)
  791. {
  792. __orderly_reboot();
  793. }
  794. static DECLARE_WORK(reboot_work, reboot_work_func);
  795. /**
  796. * orderly_reboot - Trigger an orderly system reboot
  797. *
  798. * This may be called from any context to trigger a system reboot.
  799. * If the orderly reboot fails, it will force an immediate reboot.
  800. */
  801. void orderly_reboot(void)
  802. {
  803. schedule_work(&reboot_work);
  804. }
  805. EXPORT_SYMBOL_GPL(orderly_reboot);
  806. /**
  807. * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay
  808. * @work: work_struct associated with the emergency poweroff function
  809. *
  810. * This function is called in very critical situations to force
  811. * a kernel poweroff after a configurable timeout value.
  812. */
  813. static void hw_failure_emergency_poweroff_func(struct work_struct *work)
  814. {
  815. /*
  816. * We have reached here after the emergency shutdown waiting period has
  817. * expired. This means orderly_poweroff has not been able to shut off
  818. * the system for some reason.
  819. *
  820. * Try to shut down the system immediately using kernel_power_off
  821. * if populated
  822. */
  823. pr_emerg("Hardware protection timed-out. Trying forced poweroff\n");
  824. kernel_power_off();
  825. /*
  826. * Worst of the worst case trigger emergency restart
  827. */
  828. pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
  829. emergency_restart();
  830. }
  831. static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
  832. hw_failure_emergency_poweroff_func);
  833. /**
  834. * hw_failure_emergency_poweroff - Trigger an emergency system poweroff
  835. *
  836. * This may be called from any critical situation to trigger a system shutdown
  837. * after a given period of time. If time is negative this is not scheduled.
  838. */
  839. static void hw_failure_emergency_poweroff(int poweroff_delay_ms)
  840. {
  841. if (poweroff_delay_ms <= 0)
  842. return;
  843. schedule_delayed_work(&hw_failure_emergency_poweroff_work,
  844. msecs_to_jiffies(poweroff_delay_ms));
  845. }
  846. /**
  847. * __hw_protection_shutdown - Trigger an emergency system shutdown or reboot
  848. *
  849. * @reason: Reason of emergency shutdown or reboot to be printed.
  850. * @ms_until_forced: Time to wait for orderly shutdown or reboot before
  851. * triggering it. Negative value disables the forced
  852. * shutdown or reboot.
  853. * @shutdown: If true, indicates that a shutdown will happen
  854. * after the critical tempeature is reached.
  855. * If false, indicates that a reboot will happen
  856. * after the critical tempeature is reached.
  857. *
  858. * Initiate an emergency system shutdown or reboot in order to protect
  859. * hardware from further damage. Usage examples include a thermal protection.
  860. * NOTE: The request is ignored if protection shutdown or reboot is already
  861. * pending even if the previous request has given a large timeout for forced
  862. * shutdown/reboot.
  863. */
  864. void __hw_protection_shutdown(const char *reason, int ms_until_forced, bool shutdown)
  865. {
  866. static atomic_t allow_proceed = ATOMIC_INIT(1);
  867. pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
  868. /* Shutdown should be initiated only once. */
  869. if (!atomic_dec_and_test(&allow_proceed))
  870. return;
  871. /*
  872. * Queue a backup emergency shutdown in the event of
  873. * orderly_poweroff failure
  874. */
  875. hw_failure_emergency_poweroff(ms_until_forced);
  876. if (shutdown)
  877. orderly_poweroff(true);
  878. else
  879. orderly_reboot();
  880. }
  881. EXPORT_SYMBOL_GPL(__hw_protection_shutdown);
  882. static int __init reboot_setup(char *str)
  883. {
  884. for (;;) {
  885. enum reboot_mode *mode;
  886. /*
  887. * Having anything passed on the command line via
  888. * reboot= will cause us to disable DMI checking
  889. * below.
  890. */
  891. reboot_default = 0;
  892. if (!strncmp(str, "panic_", 6)) {
  893. mode = &panic_reboot_mode;
  894. str += 6;
  895. } else {
  896. mode = &reboot_mode;
  897. }
  898. switch (*str) {
  899. case 'w':
  900. *mode = REBOOT_WARM;
  901. break;
  902. case 'c':
  903. *mode = REBOOT_COLD;
  904. break;
  905. case 'h':
  906. *mode = REBOOT_HARD;
  907. break;
  908. case 's':
  909. /*
  910. * reboot_cpu is s[mp]#### with #### being the processor
  911. * to be used for rebooting. Skip 's' or 'smp' prefix.
  912. */
  913. str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
  914. if (isdigit(str[0])) {
  915. int cpu = simple_strtoul(str, NULL, 0);
  916. if (cpu >= num_possible_cpus()) {
  917. pr_err("Ignoring the CPU number in reboot= option. "
  918. "CPU %d exceeds possible cpu number %d\n",
  919. cpu, num_possible_cpus());
  920. break;
  921. }
  922. reboot_cpu = cpu;
  923. } else
  924. *mode = REBOOT_SOFT;
  925. break;
  926. case 'g':
  927. *mode = REBOOT_GPIO;
  928. break;
  929. case 'b':
  930. case 'a':
  931. case 'k':
  932. case 't':
  933. case 'e':
  934. case 'p':
  935. reboot_type = *str;
  936. break;
  937. case 'f':
  938. reboot_force = 1;
  939. break;
  940. }
  941. str = strchr(str, ',');
  942. if (str)
  943. str++;
  944. else
  945. break;
  946. }
  947. return 1;
  948. }
  949. __setup("reboot=", reboot_setup);
  950. #ifdef CONFIG_SYSFS
  951. #define REBOOT_COLD_STR "cold"
  952. #define REBOOT_WARM_STR "warm"
  953. #define REBOOT_HARD_STR "hard"
  954. #define REBOOT_SOFT_STR "soft"
  955. #define REBOOT_GPIO_STR "gpio"
  956. #define REBOOT_UNDEFINED_STR "undefined"
  957. #define BOOT_TRIPLE_STR "triple"
  958. #define BOOT_KBD_STR "kbd"
  959. #define BOOT_BIOS_STR "bios"
  960. #define BOOT_ACPI_STR "acpi"
  961. #define BOOT_EFI_STR "efi"
  962. #define BOOT_PCI_STR "pci"
  963. static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  964. {
  965. const char *val;
  966. switch (reboot_mode) {
  967. case REBOOT_COLD:
  968. val = REBOOT_COLD_STR;
  969. break;
  970. case REBOOT_WARM:
  971. val = REBOOT_WARM_STR;
  972. break;
  973. case REBOOT_HARD:
  974. val = REBOOT_HARD_STR;
  975. break;
  976. case REBOOT_SOFT:
  977. val = REBOOT_SOFT_STR;
  978. break;
  979. case REBOOT_GPIO:
  980. val = REBOOT_GPIO_STR;
  981. break;
  982. default:
  983. val = REBOOT_UNDEFINED_STR;
  984. }
  985. return sprintf(buf, "%s\n", val);
  986. }
  987. static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
  988. const char *buf, size_t count)
  989. {
  990. if (!capable(CAP_SYS_BOOT))
  991. return -EPERM;
  992. if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR)))
  993. reboot_mode = REBOOT_COLD;
  994. else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR)))
  995. reboot_mode = REBOOT_WARM;
  996. else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR)))
  997. reboot_mode = REBOOT_HARD;
  998. else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR)))
  999. reboot_mode = REBOOT_SOFT;
  1000. else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR)))
  1001. reboot_mode = REBOOT_GPIO;
  1002. else
  1003. return -EINVAL;
  1004. reboot_default = 0;
  1005. return count;
  1006. }
  1007. static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
  1008. #ifdef CONFIG_X86
  1009. static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  1010. {
  1011. return sprintf(buf, "%d\n", reboot_force);
  1012. }
  1013. static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
  1014. const char *buf, size_t count)
  1015. {
  1016. bool res;
  1017. if (!capable(CAP_SYS_BOOT))
  1018. return -EPERM;
  1019. if (kstrtobool(buf, &res))
  1020. return -EINVAL;
  1021. reboot_default = 0;
  1022. reboot_force = res;
  1023. return count;
  1024. }
  1025. static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
  1026. static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  1027. {
  1028. const char *val;
  1029. switch (reboot_type) {
  1030. case BOOT_TRIPLE:
  1031. val = BOOT_TRIPLE_STR;
  1032. break;
  1033. case BOOT_KBD:
  1034. val = BOOT_KBD_STR;
  1035. break;
  1036. case BOOT_BIOS:
  1037. val = BOOT_BIOS_STR;
  1038. break;
  1039. case BOOT_ACPI:
  1040. val = BOOT_ACPI_STR;
  1041. break;
  1042. case BOOT_EFI:
  1043. val = BOOT_EFI_STR;
  1044. break;
  1045. case BOOT_CF9_FORCE:
  1046. val = BOOT_PCI_STR;
  1047. break;
  1048. default:
  1049. val = REBOOT_UNDEFINED_STR;
  1050. }
  1051. return sprintf(buf, "%s\n", val);
  1052. }
  1053. static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr,
  1054. const char *buf, size_t count)
  1055. {
  1056. if (!capable(CAP_SYS_BOOT))
  1057. return -EPERM;
  1058. if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR)))
  1059. reboot_type = BOOT_TRIPLE;
  1060. else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR)))
  1061. reboot_type = BOOT_KBD;
  1062. else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR)))
  1063. reboot_type = BOOT_BIOS;
  1064. else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR)))
  1065. reboot_type = BOOT_ACPI;
  1066. else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
  1067. reboot_type = BOOT_EFI;
  1068. else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
  1069. reboot_type = BOOT_CF9_FORCE;
  1070. else
  1071. return -EINVAL;
  1072. reboot_default = 0;
  1073. return count;
  1074. }
  1075. static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
  1076. #endif
  1077. #ifdef CONFIG_SMP
  1078. static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  1079. {
  1080. return sprintf(buf, "%d\n", reboot_cpu);
  1081. }
  1082. static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr,
  1083. const char *buf, size_t count)
  1084. {
  1085. unsigned int cpunum;
  1086. int rc;
  1087. if (!capable(CAP_SYS_BOOT))
  1088. return -EPERM;
  1089. rc = kstrtouint(buf, 0, &cpunum);
  1090. if (rc)
  1091. return rc;
  1092. if (cpunum >= num_possible_cpus())
  1093. return -ERANGE;
  1094. reboot_default = 0;
  1095. reboot_cpu = cpunum;
  1096. return count;
  1097. }
  1098. static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
  1099. #endif
  1100. static struct attribute *reboot_attrs[] = {
  1101. &reboot_mode_attr.attr,
  1102. #ifdef CONFIG_X86
  1103. &reboot_force_attr.attr,
  1104. &reboot_type_attr.attr,
  1105. #endif
  1106. #ifdef CONFIG_SMP
  1107. &reboot_cpu_attr.attr,
  1108. #endif
  1109. NULL,
  1110. };
  1111. #ifdef CONFIG_SYSCTL
  1112. static struct ctl_table kern_reboot_table[] = {
  1113. {
  1114. .procname = "poweroff_cmd",
  1115. .data = &poweroff_cmd,
  1116. .maxlen = POWEROFF_CMD_PATH_LEN,
  1117. .mode = 0644,
  1118. .proc_handler = proc_dostring,
  1119. },
  1120. {
  1121. .procname = "ctrl-alt-del",
  1122. .data = &C_A_D,
  1123. .maxlen = sizeof(int),
  1124. .mode = 0644,
  1125. .proc_handler = proc_dointvec,
  1126. },
  1127. };
  1128. static void __init kernel_reboot_sysctls_init(void)
  1129. {
  1130. register_sysctl_init("kernel", kern_reboot_table);
  1131. }
  1132. #else
  1133. #define kernel_reboot_sysctls_init() do { } while (0)
  1134. #endif /* CONFIG_SYSCTL */
  1135. static const struct attribute_group reboot_attr_group = {
  1136. .attrs = reboot_attrs,
  1137. };
  1138. static int __init reboot_ksysfs_init(void)
  1139. {
  1140. struct kobject *reboot_kobj;
  1141. int ret;
  1142. reboot_kobj = kobject_create_and_add("reboot", kernel_kobj);
  1143. if (!reboot_kobj)
  1144. return -ENOMEM;
  1145. ret = sysfs_create_group(reboot_kobj, &reboot_attr_group);
  1146. if (ret) {
  1147. kobject_put(reboot_kobj);
  1148. return ret;
  1149. }
  1150. kernel_reboot_sysctls_init();
  1151. return 0;
  1152. }
  1153. late_initcall(reboot_ksysfs_init);
  1154. #endif