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