Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/kernel/reboot.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2013  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt)	"reboot: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kmsg_dump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/syscore_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) int C_A_D = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct pid *cad_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) EXPORT_SYMBOL(cad_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #if defined(CONFIG_ARM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define DEFAULT_REBOOT_MODE		= REBOOT_HARD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define DEFAULT_REBOOT_MODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) EXPORT_SYMBOL_GPL(reboot_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) EXPORT_SYMBOL_GPL(panic_reboot_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * This variable is used privately to keep track of whether or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * reboot_type is still set to its default value (i.e., reboot= hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * been set on the command line).  This is needed so that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * suppress DMI scanning for reboot quirks.  Without it, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * impossible to override a faulty reboot quirk without recompiling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) int reboot_default = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) int reboot_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) enum reboot_type reboot_type = BOOT_ACPI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) int reboot_force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * If set, this is used for preparing the system to power off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) void (*pm_power_off_prepare)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) EXPORT_SYMBOL_GPL(pm_power_off_prepare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *	emergency_restart - reboot the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *	Without shutting down any hardware or taking any locks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *	reboot the system.  This is called when we know we are in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  *	trouble so this is our best effort to reboot.  This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *	safe to call in interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) void emergency_restart(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	kmsg_dump(KMSG_DUMP_EMERG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	machine_emergency_restart();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) EXPORT_SYMBOL_GPL(emergency_restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) void kernel_restart_prepare(char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	system_state = SYSTEM_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	usermodehelper_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	device_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  *	register_reboot_notifier - Register function to be called at reboot time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  *	@nb: Info about notifier function to be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *	Registers a function with the list of functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *	to be called at reboot time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *	Currently always returns zero, as blocking_notifier_chain_register()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *	always returns zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) int register_reboot_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) EXPORT_SYMBOL(register_reboot_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  *	@nb: Hook to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *	Unregisters a previously registered reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *	notifier function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  *	Returns zero on success, or %-ENOENT on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int unregister_reboot_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) EXPORT_SYMBOL(unregister_reboot_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void devm_unregister_reboot_notifier(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct notifier_block **rcnb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	rcnb = devres_alloc(devm_unregister_reboot_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			    sizeof(*rcnb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (!rcnb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ret = register_reboot_notifier(nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		*rcnb = nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		devres_add(dev, rcnb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		devres_free(rcnb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL(devm_register_reboot_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  *	Notifier list for kernel code which wants to be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  *	to restart the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  *	register_restart_handler - Register function to be called to reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  *				   the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  *	@nb: Info about handler function to be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  *	@nb->priority:	Handler priority. Handlers should follow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  *			following guidelines for setting priorities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  *			0:	Restart handler of last resort,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  *				with limited restart capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  *			128:	Default restart handler; use if no other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *				restart handler is expected to be available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  *				and/or if restart functionality is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  *				sufficient to restart the entire system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  *			255:	Highest priority restart handler, will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  *				preempt all other restart handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  *	Registers a function with code to be called to restart the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  *	system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  *	Registered functions will be called from machine_restart as last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  *	step of the restart sequence (if the architecture specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  *	machine_restart function calls do_kernel_restart - see below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  *	for details).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *	Registered functions are expected to restart the system immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *	If more than one function is registered, the restart handler priority
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *	selects which function will be called first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *	Restart handlers are expected to be registered from non-architecture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  *	code, typically from drivers. A typical use case would be a system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  *	where restart functionality is provided through a watchdog. Multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  *	restart handlers may exist; for example, one restart handler might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  *	restart the entire system, while another only restarts the CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  *	In such cases, the restart handler which only restarts part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  *	hardware is expected to register with low priority to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  *	it only runs if no other means to restart the system is available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *	Currently always returns zero, as atomic_notifier_chain_register()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  *	always returns zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int register_restart_handler(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	return atomic_notifier_chain_register(&restart_handler_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) EXPORT_SYMBOL(register_restart_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  *	unregister_restart_handler - Unregister previously registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  *				     restart handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  *	@nb: Hook to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  *	Unregisters a previously registered restart handler function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  *	Returns zero on success, or %-ENOENT on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int unregister_restart_handler(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) EXPORT_SYMBOL(unregister_restart_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  *	do_kernel_restart - Execute kernel restart handler call chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  *	Calls functions registered with register_restart_handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  *	Expected to be called from machine_restart as last step of the restart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  *	sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  *	Restarts the system immediately if a restart handler function has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  *	registered. Otherwise does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) void do_kernel_restart(char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #ifdef CONFIG_NO_GKI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static ATOMIC_NOTIFIER_HEAD(pre_restart_handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int register_pre_restart_handler(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return atomic_notifier_chain_register(&pre_restart_handler_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) EXPORT_SYMBOL(register_pre_restart_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int unregister_pre_restart_handler(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return atomic_notifier_chain_unregister(&pre_restart_handler_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) EXPORT_SYMBOL(unregister_pre_restart_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) void do_kernel_pre_restart(char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	atomic_notifier_call_chain(&pre_restart_handler_list, reboot_mode, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) void migrate_to_reboot_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/* The boot cpu is always logical cpu 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	int cpu = reboot_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	cpu_hotplug_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	/* Make certain the cpu I'm about to reboot on is online */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (!cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		cpu = cpumask_first(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	/* Prevent races with other tasks migrating this task */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	current->flags |= PF_NO_SETAFFINITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* Make certain I only run on the appropriate processor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  *	kernel_restart - reboot the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  *	@cmd: pointer to buffer containing command to execute for restart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *		or %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  *	Shutdown everything and perform a clean reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  *	This is not safe to call in interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) void kernel_restart(char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	kernel_restart_prepare(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	migrate_to_reboot_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	syscore_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		pr_emerg("Restarting system\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		pr_emerg("Restarting system with command '%s'\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	machine_restart(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) EXPORT_SYMBOL_GPL(kernel_restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static void kernel_shutdown_prepare(enum system_states state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	blocking_notifier_call_chain(&reboot_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	system_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	usermodehelper_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	device_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  *	kernel_halt - halt the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  *	Shutdown everything and perform a clean system halt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) void kernel_halt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	kernel_shutdown_prepare(SYSTEM_HALT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	migrate_to_reboot_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	syscore_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	pr_emerg("System halted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	machine_halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) EXPORT_SYMBOL_GPL(kernel_halt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  *	kernel_power_off - power_off the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *	Shutdown everything and perform a clean system power_off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) void kernel_power_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (pm_power_off_prepare)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		pm_power_off_prepare();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	migrate_to_reboot_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	syscore_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	pr_emerg("Power down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	machine_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) EXPORT_SYMBOL_GPL(kernel_power_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) DEFINE_MUTEX(system_transition_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * Reboot system call: for obvious reasons only root may call it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  * and even root needs to set up some magic numbers in the registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  * so that some mistake won't make this reboot the whole machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  * You can also set the meaning of the ctrl-alt-del-key here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * reboot doesn't sync: do that yourself before calling this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		void __user *, arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	char buffer[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	/* We only trust the superuser with rebooting the system. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	/* For safety, we require "magic" arguments. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			(magic2 != LINUX_REBOOT_MAGIC2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			magic2 != LINUX_REBOOT_MAGIC2A &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			magic2 != LINUX_REBOOT_MAGIC2B &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			magic2 != LINUX_REBOOT_MAGIC2C))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	 * If pid namespaces are enabled and the current task is in a child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	 * call do_exit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	ret = reboot_pid_ns(pid_ns, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	/* Instead of trying to make the power_off code look like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	 * halt when pm_power_off is not set do it the easy way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		cmd = LINUX_REBOOT_CMD_HALT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	mutex_lock(&system_transition_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	case LINUX_REBOOT_CMD_RESTART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		kernel_restart(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	case LINUX_REBOOT_CMD_CAD_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		C_A_D = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	case LINUX_REBOOT_CMD_CAD_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		C_A_D = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	case LINUX_REBOOT_CMD_HALT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		kernel_halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		do_exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		panic("cannot halt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	case LINUX_REBOOT_CMD_POWER_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		kernel_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		do_exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	case LINUX_REBOOT_CMD_RESTART2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		buffer[sizeof(buffer) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		kernel_restart(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #ifdef CONFIG_KEXEC_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	case LINUX_REBOOT_CMD_KEXEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		ret = kernel_kexec();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) #ifdef CONFIG_HIBERNATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	case LINUX_REBOOT_CMD_SW_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		ret = hibernate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	mutex_unlock(&system_transition_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static void deferred_cad(struct work_struct *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	kernel_restart(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  * As it's called within an interrupt, it may NOT sync: the only choice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  * is whether to reboot at once, or just ignore the ctrl-alt-del.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) void ctrl_alt_del(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	static DECLARE_WORK(cad_work, deferred_cad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (C_A_D)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		schedule_work(&cad_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		kill_cad_pid(SIGINT, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) static const char reboot_cmd[] = "/sbin/reboot";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static int run_cmd(const char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	char **argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	static char *envp[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		"HOME=/",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	argv = argv_split(GFP_KERNEL, cmd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (argv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		argv_free(argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static int __orderly_reboot(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	ret = run_cmd(reboot_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		emergency_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		kernel_restart(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static int __orderly_poweroff(bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	ret = run_cmd(poweroff_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (ret && force) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		 * I guess this should try to kick off some daemon to sync and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		 * poweroff asap.  Or not even bother syncing if we're doing an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		 * emergency shutdown?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		emergency_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		kernel_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static bool poweroff_force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static void poweroff_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	__orderly_poweroff(poweroff_force);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static DECLARE_WORK(poweroff_work, poweroff_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  * orderly_poweroff - Trigger an orderly system poweroff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  * @force: force poweroff if command execution fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)  * This may be called from any context to trigger a system shutdown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  * If the orderly shutdown fails, it will force an immediate shutdown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) void orderly_poweroff(bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (force) /* do not override the pending "true" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		poweroff_force = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	schedule_work(&poweroff_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) EXPORT_SYMBOL_GPL(orderly_poweroff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static void reboot_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	__orderly_reboot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static DECLARE_WORK(reboot_work, reboot_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  * orderly_reboot - Trigger an orderly system reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  * This may be called from any context to trigger a system reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)  * If the orderly reboot fails, it will force an immediate reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) void orderly_reboot(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	schedule_work(&reboot_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) EXPORT_SYMBOL_GPL(orderly_reboot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static int __init reboot_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		enum reboot_mode *mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		 * Having anything passed on the command line via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		 * reboot= will cause us to disable DMI checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		 * below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		reboot_default = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		if (!strncmp(str, "panic_", 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			mode = &panic_reboot_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			str += 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			mode = &reboot_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		switch (*str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		case 'w':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			*mode = REBOOT_WARM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			*mode = REBOOT_COLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			*mode = REBOOT_HARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			if (isdigit(*(str+1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 				reboot_cpu = simple_strtoul(str+1, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			else if (str[1] == 'm' && str[2] == 'p' &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 							isdigit(*(str+3)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 				reboot_cpu = simple_strtoul(str+3, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 				*mode = REBOOT_SOFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			if (reboot_cpu >= num_possible_cpus()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 				pr_err("Ignoring the CPU number in reboot= option. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 				       "CPU %d exceeds possible cpu number %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 				       reboot_cpu, num_possible_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 				reboot_cpu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		case 'g':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			*mode = REBOOT_GPIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		case 'b':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		case 'a':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		case 'e':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			reboot_type = *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			reboot_force = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		str = strchr(str, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		if (str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 			str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) __setup("reboot=", reboot_setup);