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