Skip to contentSkip to conversation

'khelper' (child) is stuck in endless loop: do_signal() and !user_mode(regs)

DISCUSSION Dmitry ADAMUSHKA (EXT) 2 participants 2 messages Mar 8, 2012 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 139779962.60750.1331202718116.JavaMail.root@storentr1.softathome.com

#1 lore raw

Oleg,

I'm able to reproduce this problem on x86 (32 bits) with the following patches that try to simulate the real-life situation (see the comments in the patches).

It happens only when CONFIG_VM86 is disabled (I tried both). Supposedly, due to the following bits of the VM86-specific code that let us break out of the endless-loop.

#ifdef CONFIG_VM86
#define resume_userspace_sig check_userspace
#else
[...]

there is the specific are-we-a-kernel-task? check here

check_userspace:
movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
movb PT_CS(%esp), %al
andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax cmpl $USER_RPL, %eax
jb resume_kernel # not returning to v8086 or userspace

ENTRY(resume_userspace)
LOCKDEP_SYS_EXIT
[...]
jne work_pending
jmp restore_all

which is available neither in case of !CONFIG_VM86, nor in case of MIPS. Hence, the loop.

So here are the patches to simulate the problem. Is this approach not valid for one or another reason?

Thanks in advance.

=== copy-pasted ===

kmod.c 2012-03-08 11:25:05.028661835 +0100 renamed from kmod.c.orig 2012-03-08 10:26:05.504752023 +0100 +22 -0
@@ -154,6 +154,15 @@ static int ____call_usermodehelper(void
        /* We can run anywhere, unlike our parent keventd(). */
        set_cpus_allowed_ptr(current, cpu_all_mask);
 
+       printk(KERN_EMERG "Unleash the signal...\n");
+
+       /*
+        * (1) here we emulate receiving a signal.
+        *     In the original case, a signal should be delivered from outside,
+        *     say, by "kill(-1, SIGKILL)" in busybox.
+        */
+       send_sig(SIGUSR1, current, 0);
+
        /*
         * Our parent is keventd, which runs with elevated scheduling priority.
         * Avoid propagating that into the userspace child.
@@ -181,6 +190,19 @@ static int ____call_usermodehelper(void
 
        commit_creds(new);
 
+       /* (2) here we emulate the failure of kernel_execve().
+        *     In real life, the failure can be due to a memory shortage,
+        *     or something else.
+         *     In our case, it happens when a board reboots - same as (1) above.
+        */
+       retval = kernel_execve(NULL,
+                              (const char *const *)sub_info->argv,
+                              (const char *const *)sub_info->envp);
+
+       printk(KERN_EMERG "x86 is rock-solid!");
+       flush_signals(current);
+
+       /* If we survived the test, let's continue so the user should not notice. */
        retval = kernel_execve(sub_info->path,
                               (const char *const *)sub_info->argv,
                               (const char *const *)sub_info->envp);

and another one

x86/kernel/signal.c 2012-03-08 10:31:18.682304346 +0100 renamed from x86/kernel/signal.c.orig 2012-03-08 11:18:19.702651943 +0100 +4 -1
@@ -765,8 +765,11 @@ static void do_signal(struct pt_regs *re
         * X86_32: vm86 regs switched out by assembly code before reaching
         * here, so testing against kernel CS suffices.
         */
-       if (!user_mode(regs))
+       if (!user_mode(regs)) {
+               printk(KERN_EMERG "* endless loop\n");
+               dump_stack();
                return;
+       }
 
        signr = get_signal_to_deliver(&info, &ka, regs, NULL);
        if (signr > 0) {

----- Original Message -----

Quoted 68 lines

From: "Dmitry Adamushko" <[email protected]>
To: "Oleg Nesterov" <[email protected]>
Cc: "Dmitry ADAMUSHKA (EXT)" <[email protected]>, "Ingo Molnar" <[email protected]>, "Ralf Baechle" <[email protected]>, "wouter cloetens" <[email protected]>, [email protected]
Sent: Wednesday, March 7, 2012 9:05:43 PM
Subject: Re: 'khelper' (child) is stuck in endless loop: do_signal() and !user_mode(regs)

Hi Oleg,

Quoted 5 lines · Dmitry ADAMUSHKA (EXT)
Quoted 4 lines

Now, the assumptions (the question is whether these are true for the recent kernels):

Quoted 7 lines
Quoted 4 lines

1) TIF_SIGPENDING can be set for 'khelper' while it's running in ____call_usermodehelper()
   between (a) flush_signal_handlers() and (b) kernel_execve() => so TIF_SIGPENDING is set;

Yes, but it is not khelper. It is another kernel thread. Yes, its ->comm[] was copied from parent, so ps/etc can show it as khelper.

Sure, that's why I indicated 'khelper' (child).

Quoted 16 lines
Quoted 16 lines

2) kernel_execve() can fail in ____call_usermodehelper().

The later one is less of an assumption; let's say, it fails due to a shortage of memory (or whatever).

If (1) is true, then

the pre-conditions:

- a kernel space task;

'khelper' running ____call_usermodehelper() in our case.

- TIF_SIGPENDING is set.

Quoted 8 lines
Quoted 6 lines

A signal has been delivered, say, as a result of kill(-1, SIGKILL).

The endless loop is as follows:

* syscall_exit_work:
 - work_pending: // start_of_the_loop

We shouldn't be here. This is the kernel thread.

Note that kernel_execve() is backed up by a full fledged syscall (not just a function call, at least on MIPS and x86), so I assume that all the usual syscall-related stuff applies here as well.

Quoted 9 lines

And if start_thread() was already called, then

Quoted 4 lines

 - work_notify_sig:
   - do_notify_resume()
     - do_signal() ==> if (!user_mode(regs)) return; so signals are not handled

user_mode() is no longer true.

!user_mode() is true. Note, the failure of kernel_execve() is one of the pre-conditions. So we have a kernel thread returning from a real syscall (hence, syscall_exit and co) with TIF_SIGPENDING.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Once again, I can be wrong, I'll read this email tomorrow.

Great, thanks!

-- Dmitry

This message and any attachments herein are confidential, intended solely for the addressees and are SoftAtHome's ownership. Any unauthorized use or dissemination is prohibited. If you are not the intended addressee of this message, please cancel it immediately and inform the sender.

kmod.c renamed from kmod.c.orig +22 -0
@@ -154,6 +154,15 @@ static int ____call_usermodehelper(void
 	/* We can run anywhere, unlike our parent keventd(). */
 	set_cpus_allowed_ptr(current, cpu_all_mask);
 
+	printk(KERN_EMERG "Unleash the signal...\n");
+
+	/*
+ 	 * (1) here we emulate receiving a signal.
+	 *     In the original case, a signal should be delivered from outside,
+	 *     say, by "kill(-1, SIGKILL)" in busybox.
+	 */
+	send_sig(SIGUSR1, current, 0);
+
 	/*
 	 * Our parent is keventd, which runs with elevated scheduling priority.
 	 * Avoid propagating that into the userspace child.
@@ -181,6 +190,19 @@ static int ____call_usermodehelper(void
 
 	commit_creds(new);
 
+	/* (2) here we emulate the failure of kernel_execve().
+	 *     In real life, the failure can be due to a memory shortage,
+	 *     or something else.
+         *     In our case, it happens when a board reboots - same as (1) above.
+	 */
+	retval = kernel_execve(NULL,
+			       (const char *const *)sub_info->argv,
+			       (const char *const *)sub_info->envp);
+
+	printk(KERN_EMERG "x86 is rock-solid!");
+	flush_signals(current);
+
+	/* If we survived the test, let's continue so the user should not notice. */
 	retval = kernel_execve(sub_info->path,
 			       (const char *const *)sub_info->argv,
 			       (const char *const *)sub_info->envp);
x86/kernel/signal.c renamed from x86/kernel/signal.c.orig +4 -1
@@ -765,8 +765,11 @@ static void do_signal(struct pt_regs *re
 	 * X86_32: vm86 regs switched out by assembly code before reaching
 	 * here, so testing against kernel CS suffices.
 	 */
-	if (!user_mode(regs))
+	if (!user_mode(regs)) {
+		printk(KERN_EMERG "* endless loop\n");
+		dump_stack();
 		return;
+	}
 
 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
 	if (signr > 0) {
#2 lore raw

Hi Dmitry,

I think you are right, but I am not expert. Add Peter.

Quoted 4 lines · Dmitry ADAMUSHKA (EXT)

Oleg,

I'm able to reproduce this problem on x86 (32 bits)

And I guess "32 bits" is important. arch/x86/kernel/sys_i386_32.c:kernel_execve() does "int 0x80".

If do_execve() fails before start_thread() and TIF_SIGPENDING is set, entry_32.S calls do_notify_resume() and we lost.

I guess this is what you meant from the very beginning ;)

Quoted 9 lines

It happens only when CONFIG_VM86 is disabled (I tried both). Supposedly, due to the following bits of the VM86-specific code that let us break out of the endless-loop.

#ifdef CONFIG_VM86
#define resume_userspace_sig check_userspace
#else
[...]

Quoted 8 lines

there is the specific are-we-a-kernel-task? check here

check_userspace:
movl PT_EFLAGS(%esp), %eax # mix EFLAGS and C
movb PT_CS(%esp), %al
andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax cmpl $USER_RPL, %eax
jb resume_kernel # not returning to v8086 or userspace

Agreed, we need the USER_RPL check.

Quoted 12 lines

So here are the patches to simulate the problem. Is this approach not valid for one or another reason?

Thanks in advance.

=== copy-pasted ===

kmod.c 2012-03-08 11:25:05.028661835 +0100 renamed from kmod.c.orig 2012-03-08 10:26:05.504752023 +0100 +0 -0
@@ -154,6 +154,15 @@ static int ____call_usermodehelper(void
        /* We can run anywhere, unlike our parent keventd(). */

kmod.c 2012-03-08 11:25:05.028661835 +0100 static int ____call_usermodehelper(void · line 164

Quoted 10 lines
kmod.c 2012-03-08 11:25:05.028661835 +0100 renamed from kmod.c.orig 2012-03-08 10:26:05.504752023 +0100 +8 -0
        set_cpus_allowed_ptr(current, cpu_all_mask);
 
+       printk(KERN_EMERG "Unleash the signal...\n");
+
+       /*
+        * (1) here we emulate receiving a signal.
+        *     In the original case, a signal should be delivered from outside,
+        *     say, by "kill(-1, SIGKILL)" in busybox.
+        */
+       send_sig(SIGUSR1, current, 0);

Yes, this kills the task, kernel_execve() can't succeed,

Quoted 5 lines
kmod.c 2012-03-08 11:25:05.028661835 +0100 renamed from kmod.c.orig 2012-03-08 10:26:05.504752023 +0100 +0 -0
        /*
         * Our parent is keventd, which runs with elevated scheduling priority.
         * Avoid propagating that into the userspace child.
@@ -181,6 +190,19 @@ static int ____call_usermodehelper(void
 

kmod.c 2012-03-08 11:25:05.028661835 +0100 static int ____call_usermodehelper(void · line 200

Quoted 10 lines
kmod.c 2012-03-08 11:25:05.028661835 +0100 renamed from kmod.c.orig 2012-03-08 10:26:05.504752023 +0100 +8 -0
        commit_creds(new);
 
+       /* (2) here we emulate the failure of kernel_execve().
+        *     In real life, the failure can be due to a memory shortage,
+        *     or something else.
+         *     In our case, it happens when a board reboots - same as (1) above.
+        */
+       retval = kernel_execve(NULL,
+                              (const char *const *)sub_info->argv,
+                              (const char *const *)sub_info->envp);

and I guess it can't even return.

Quoted 13 lines
kmod.c 2012-03-08 11:25:05.028661835 +0100 renamed from kmod.c.orig 2012-03-08 10:26:05.504752023 +0100 +4 -0
+       printk(KERN_EMERG "x86 is rock-solid!");
+       flush_signals(current);
+
+       /* If we survived the test, let's continue so the user should not notice. */
        retval = kernel_execve(sub_info->path,
                               (const char *const *)sub_info->argv,
                               (const char *const *)sub_info->envp);

and another one

x86/kernel/signal.c 2012-03-08 10:31:18.682304346 +0100 static void do_signal(struct pt_regs *re · line 772

Quoted 9 lines
x86/kernel/signal.c 2012-03-08 10:31:18.682304346 +0100 renamed from x86/kernel/signal.c.orig 2012-03-08 11:18:19.702651943 +0100 +4 -1
@@ -765,8 +765,11 @@ static void do_signal(struct pt_regs *re
         * X86_32: vm86 regs switched out by assembly code before reaching
         * here, so testing against kernel CS suffices.
         */
-       if (!user_mode(regs))
+       if (!user_mode(regs)) {
+               printk(KERN_EMERG "* endless loop\n");
+               dump_stack();
                return;
+       }

so yes, it enters the endless loop.

I do not know what should be fixed, kernel_execve() or system_call paths.

Oleg.