Skip to contentSkip to conversation

Kernel threads

DISCUSSION Oleg Nesterov 2 participants 5 messages Mar 8, 2007 – Mar 9, 2007

lore
Share & tools
lore
#0 lore raw
Quoted 19 lines · Andrew Morton
Quoted 6 lines · On Tue, 6 Mar 2007 11:03:48 -0500 "linux-os \(Dick Johnson\)"…

In linux-2.6.16.24, there is a problem with kernel threads and the aic79xx.c driver.

Quoted 12 lines

When nash is executing /initrd/linuxrc in the initial RAM disk during boot, it will be installing drivers. One driver, aic79xx.c creates some kernel threads that will exit after the initialization procedure. Actually the number of tasks depends upon the number of disks found as the driver spawns these tasks so initialization can occur in the background. The kernel tasks have been 'parented' to init. This may be fine for the real init, but nash and other shells receive the SIGCHLD signal and think that the fork()/exec() they have executed is complete. This makes nash insert drivers when the needed previous ones have not yet initialized. Also, when booting a shell, the signals from the exiting kernel tasks confuse it.

Quoted 2 lines

ug. I've always disliked the kernel's dependence upon init to reap exitted kernel threads. It Just Seems Wrong.

Yes. It is ugly to kick init to reap the kernel thread. I was going to do this change a long ago, but I don't have time to do even a minimal testing now.

reparent_to_init() expects that thread_group_empty() == true anyway, we can set ->exit_signal = -1 and make this thread invisible to init.

Roland, what do you think?

Oleg.

kernel/exit.c renamed from kernel/exit.c~rti +2 -2
@@ -276,8 +276,8 @@ static void reparent_to_init(void)
 	current->parent = child_reaper(current);
 	add_parent(current);
 
-	/* Set the exit signal to SIGCHLD so we signal init on exit */
-	current->exit_signal = SIGCHLD;
+	/* make the task autoreap */
+	current->exit_signal = -1;
 
 	if (!has_rt_policy(current) && (task_nice(current) < 0))
 		set_user_nice(current, 0);
#1 lore raw

Your change seems fine to me. I certainly concur that it seems insane for init to be responsible for tasks created magically inside the kernel. The history I've found says that the setting to SIGCHLD was introduced as part of "v2.5.1.9 -> v2.5.1.10", without detailed commentary in the log. This was probably before the auto-reaping semantics worked as they do now. So like the man said, at the time, it seemed the logical thing to do.

To be paranoid, I wouldn't make this change in any stable kernel series. It changes behavior visible to userland (init) from how it has been consistently for five years, so, who knows, something might notice. The old behavior is pretty harmless, albeit changing it seems both preferable and harmless.

Thanks,
Roland

#2 lore raw
Quoted 9 lines · Roland McGrath

Your change seems fine to me. I certainly concur that it seems insane for init to be responsible for tasks created magically inside the kernel. The history I've found says that the setting to SIGCHLD was introduced as part of "v2.5.1.9 -> v2.5.1.10", without detailed commentary in the log. This was probably before the auto-reaping semantics worked as they do now. So like the man said, at the time, it seemed the logical thing to do.

Quoted 5 lines

To be paranoid, I wouldn't make this change in any stable kernel series. It changes behavior visible to userland (init) from how it has been consistently for five years, so, who knows, something might notice. The old behavior is pretty harmless, albeit changing it seems both preferable and harmless.

Yes sure, this change shoud be tested in -mm tree (I'll send the patch on Sunday after some testing). The only (afaics) problem is that with this change a kernel thread must not do do_fork(CLONE_THREAD). I think it should not, but currently this is technically possible. Perhaps it makes sense to add BUG_ON(CLONE_THREAD && group_leader->exit_signal==-1) in copy_process().

On a related note,

	zap_other_threads:

		if (t != p->group_leader)
			t->exit_signal = -1;

looks like another leftover to me, we already depend on the fact that all sub-threads have ->exit_signal == -1 (otherwise, for example, a thread group just can't exit properly).

While we are talking about kernel threads, there is something I can't undestand. kthread/daemonize use sigprocmask(SIG_BLOCK) to protect against signals. This doesn't look right to me, because this doesn't prevent the signal delivery, this only blocks signal_wake_up(). Every "killall -33 khelper" means a "struct siginfo" leak.

Imho, the kernel thread shouldn't play with ->blocked at all. Instead it should set SIG_IGN for all handlers. If it really needs, say, SIGCHLD, it should call allow_signal() anyway. Do you see any problems with this approach?

Oleg.

#3 lore raw
Quoted 3 lines

Yes sure, this change shoud be tested in -mm tree (I'll send the patch on Sunday after some testing). The only (afaics) problem is that with this change a kernel thread must not do do_fork(CLONE_THREAD).

To clarify, the danger here is that an exit_signal=-1 leader would self-reap and leave behind live threads with dangling ->group_leader pointers. This danger doesn't exist for normal user group leaders with parents ignoring SIGCHLD, because exit_signal is never set to -1 until do_notify_parent, which is never called until the last thread in the group dies (except when ptrace'd, but then do_notify_parent never resets exit_signal at all). Is that right?

Quoted 3 lines

I think it should not, but currently this is technically possible. Perhaps it makes sense to add BUG_ON(CLONE_THREAD && group_leader->exit_signal==-1) in copy_process().

It probably wouldn't hurt to make it:

	if (user_mode(regs))
		BUG_ON(current->group_leader->exit_signal == -1);
	else
		BUG_ON((clone_flags & (CLONE_THREAD|CLONE_UNTRACED))
		       != CLONE_UNTRACED);
Quoted 8 lines
	zap_other_threads:

		if (t != p->group_leader)
			t->exit_signal = -1;

looks like another leftover to me, we already depend on the fact that all sub-threads have ->exit_signal == -1 (otherwise, for example, a thread group just can't exit properly).

Yes, I agree it looks superfluous.

Quoted 5 lines

While we are talking about kernel threads, there is something I can't undestand. kthread/daemonize use sigprocmask(SIG_BLOCK) to protect against signals. This doesn't look right to me, because this doesn't prevent the signal delivery, this only blocks signal_wake_up(). Every "killall -33 khelper" means a "struct siginfo" leak.

It does prevent the delivery (signal_pending() never set), but not the queuing.

Quoted 4 lines

Imho, the kernel thread shouldn't play with ->blocked at all. Instead it should set SIG_IGN for all handlers. If it really needs, say, SIGCHLD, it should call allow_signal() anyway. Do you see any problems with this approach?

That sounds reasonable to me generally. However, if kernel threads ever spawn user children, they may not want the self-reaping behavior of ignoring SIGCHLD even if they never dequeue the signal (because they want to call do_wait). There might be other strange caveats like that I'm not thinking of.

Thanks,
Roland

#4 lore raw
Quoted 5 lines · Roland McGrath
Quoted 4 lines

Yes sure, this change shoud be tested in -mm tree (I'll send the patch on Sunday after some testing). The only (afaics) problem is that with this change a kernel thread must not do do_fork(CLONE_THREAD).

Quoted 7 lines

To clarify, the danger here is that an exit_signal=-1 leader would self-reap and leave behind live threads with dangling ->group_leader pointers. This danger doesn't exist for normal user group leaders with parents ignoring SIGCHLD, because exit_signal is never set to -1 until do_notify_parent, which is never called until the last thread in the group dies (except when ptrace'd, but then do_notify_parent never resets exit_signal at all). Is that right?

I think yes.

Quoted 8 lines
Quoted 3 lines

I think it should not, but currently this is technically possible. Perhaps it makes sense to add BUG_ON(CLONE_THREAD && group_leader->exit_signal==-1) in copy_process().

It probably wouldn't hurt to make it:

	if (user_mode(regs))
		BUG_ON(current->group_leader->exit_signal == -1);

Well, this is of course right, but a bit strange. Because we can add this check to any function which can't be called after exit_notify().

Quoted 3 lines
	else
		BUG_ON((clone_flags & (CLONE_THREAD|CLONE_UNTRACED))
		       != CLONE_UNTRACED);

I think this _should_ be right, but please note that fork_idle() does copy_process(CLONE_VM). Also, we may have some external driver which plays with do_fork/copy_process.

Quoted 7 lines
Quoted 5 lines

While we are talking about kernel threads, there is something I can't undestand. kthread/daemonize use sigprocmask(SIG_BLOCK) to protect against signals. This doesn't look right to me, because this doesn't prevent the signal delivery, this only blocks signal_wake_up(). Every "killall -33 khelper" means a "struct siginfo" leak.

It does prevent the delivery (signal_pending() never set), but not the queuing.

Yep.

Quoted 9 lines
Quoted 4 lines

Imho, the kernel thread shouldn't play with ->blocked at all. Instead it should set SIG_IGN for all handlers. If it really needs, say, SIGCHLD, it should call allow_signal() anyway. Do you see any problems with this approach?

That sounds reasonable to me generally. However, if kernel threads ever spawn user children, they may not want the self-reaping behavior of ignoring SIGCHLD even if they never dequeue the signal (because they want to call do_wait).

Yes. That is why wait_for_helper() does allow_signal(SIGCHLD). I think a kernel thread must not make any assumption about ->action[SIGCHLD] if it wants to call wait4, but we may break some "buggy" external driver.

In fact, most threads inherit action[SIGCHLD] == SIG_IGN from worker_thread().

BTW, wait_for_helper() does do_sigaction() before allow_signal(). Looks unneeded to me.

Quoted 2 lines

There might be other strange caveats like that I'm not
thinking of.

Yes, this makes me worry too :)

Oleg.