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