Skip to content

Commit 2ca067e

Browse files
oleg-nesterovtorvalds
authored andcommitted
poweroff: change orderly_poweroff() to use schedule_work()
David said: Commit 6c0c0d4 ("poweroff: fix bug in orderly_poweroff()") apparently fixes one bug in orderly_poweroff(), but introduces another. The comments on orderly_poweroff() claim it can be called from any context - and indeed we call it from interrupt context in arch/powerpc/platforms/pseries/ras.c for example. But since that commit this is no longer safe, since call_usermodehelper_fns() is not safe in interrupt context without the UMH_NO_WAIT option. orderly_poweroff() can be used from any context but UMH_WAIT_EXEC is sleepable. Move the "force" logic into __orderly_poweroff() and change orderly_poweroff() to use the global poweroff_work which simply calls __orderly_poweroff(). While at it, remove the unneeded "int argc" and change argv_split() to use GFP_KERNEL. We use the global "bool poweroff_force" to pass the argument, this can obviously affect the previous request if it is pending/running. So we only allow the "false => true" transition assuming that the pending "true" should succeed anyway. If schedule_work() fails after that we know that work->func() was not called yet, it must see the new value. This means that orderly_poweroff() becomes async even if we do not run the command and always succeeds, schedule_work() can only fail if the work is already pending. We can export __orderly_poweroff() and change the non-atomic callers which want the old semantics. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reported-by: Benjamin Herrenschmidt <benh@kernel.crashing.org> Reported-by: David Gibson <david@gibson.dropbear.id.au> Cc: Lucas De Marchi <lucas.demarchi@profusion.mobi> Cc: Feng Hong <hongfeng@marvell.com> Cc: Kees Cook <keescook@chromium.org> Cc: Serge Hallyn <serge.hallyn@canonical.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: "Rafael J. Wysocki" <rjw@sisk.pl> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d002858 commit 2ca067e

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

‎kernel/sys.c‎

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,9 +2185,8 @@ SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
21852185

21862186
char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
21872187

2188-
static int __orderly_poweroff(void)
2188+
static int __orderly_poweroff(bool force)
21892189
{
2190-
int argc;
21912190
char **argv;
21922191
static char *envp[] = {
21932192
"HOME=/",
@@ -2196,20 +2195,40 @@ static int __orderly_poweroff(void)
21962195
};
21972196
int ret;
21982197

2199-
argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
2200-
if (argv == NULL) {
2198+
argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
2199+
if (argv) {
2200+
ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
2201+
argv_free(argv);
2202+
} else {
22012203
printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2202-
__func__, poweroff_cmd);
2203-
return -ENOMEM;
2204+
__func__, poweroff_cmd);
2205+
ret = -ENOMEM;
22042206
}
22052207

2206-
ret = call_usermodehelper_fns(argv[0], argv, envp, UMH_WAIT_EXEC,
2207-
NULL, NULL, NULL);
2208-
argv_free(argv);
2208+
if (ret && force) {
2209+
printk(KERN_WARNING "Failed to start orderly shutdown: "
2210+
"forcing the issue\n");
2211+
/*
2212+
* I guess this should try to kick off some daemon to sync and
2213+
* poweroff asap. Or not even bother syncing if we're doing an
2214+
* emergency shutdown?
2215+
*/
2216+
emergency_sync();
2217+
kernel_power_off();
2218+
}
22092219

22102220
return ret;
22112221
}
22122222

2223+
static bool poweroff_force;
2224+
2225+
static void poweroff_work_func(struct work_struct *work)
2226+
{
2227+
__orderly_poweroff(poweroff_force);
2228+
}
2229+
2230+
static DECLARE_WORK(poweroff_work, poweroff_work_func);
2231+
22132232
/**
22142233
* orderly_poweroff - Trigger an orderly system poweroff
22152234
* @force: force poweroff if command execution fails
@@ -2219,21 +2238,9 @@ static int __orderly_poweroff(void)
22192238
*/
22202239
int orderly_poweroff(bool force)
22212240
{
2222-
int ret = __orderly_poweroff();
2223-
2224-
if (ret && force) {
2225-
printk(KERN_WARNING "Failed to start orderly shutdown: "
2226-
"forcing the issue\n");
2227-
2228-
/*
2229-
* I guess this should try to kick off some daemon to sync and
2230-
* poweroff asap. Or not even bother syncing if we're doing an
2231-
* emergency shutdown?
2232-
*/
2233-
emergency_sync();
2234-
kernel_power_off();
2235-
}
2236-
2237-
return ret;
2241+
if (force) /* do not override the pending "true" */
2242+
poweroff_force = true;
2243+
schedule_work(&poweroff_work);
2244+
return 0;
22382245
}
22392246
EXPORT_SYMBOL_GPL(orderly_poweroff);

0 commit comments

Comments
 (0)