projects/linux·blob v6.0 · refs

1294 lines · 31.7 KiB

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