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