reparent/untrace: do nothing if no childs/tracees
Oleg Nesterov
maintainerPATCH 4/4 reparent/untrace: do nothing if no childs/tracees
forget_original_parent() and exit_ptrace() can avoid taking the global tasklist_lock if there are no childs/tracees. But I failed to invent the comment to explain why/when this is safe to do, that is why the separate patch/changelog.
The problem is, we can race with the concurrent release_task() which can remove the last child form our ->children/ptraced list. This means that list_empty() can return the "false" positive, it is possible that release_task() is still in progress, it can use the caller's task_struct somehow, and it is even possible that list_del(sibling/ptrace_entry) has not yet completed.
But this is fine, before our task_struct will be released we will take tasklist_lock at least once in release_task(), this will synchronize us with the possible release_task/ptrace_unlink in flight.
However, forget_original_parent() has another problem. We can race with another thread which has already picked us for reparenting before we set PF_EXITING, so this patch also checks thread_group_empty().
It is possible to be more clever, we can take tasklist for reading, or ensure that ->thread_group.prev is not PF_EXITING, but this is nasty. Perhaps even this optimization is too ugly.
kernel/exit.c renamed from kernel/exit.c~4_TASKLIST +10 -0
@@ -803,6 +803,16 @@ static void forget_original_parent(strucstruct task_struct *p, *n, *reaper;LIST_HEAD (dead_childs); +if (thread_group_empty (father)) { +/* +* Make sure no other thread can reparent to +* us after the list_empty(->children) check. +*/ +smp_rmb (); +if (list_empty (&father->children)) +return ; + } +write_lock_irq (&tasklist_lock); reaper =find_new_reaper (father);
kernel/ptrace.c renamed from kernel/ptrace.c~4_TASKLIST +3 -0
@@ -323,6 +323,9 @@ void exit_ptrace(struct task_struct *trastruct task_struct *p, *n;LIST_HEAD (ptrace_dead); +if (list_empty (&tracer->ptraced)) +return ; +write_lock_irq (&tasklist_lock);list_for_each_entry_safe (p, n, &tracer->ptraced, ptrace_entry) {if (__ptrace_detach (tracer, p))
Oleg Nesterov
maintainerQuoted 8 lines · Oleg Nesterov
However,
forget_original_parent()has another problem. We can race with another thread which has already picked us for reparenting before we setPF_EXITING, so this patch also checksthread_group_empty().It is possible to be more clever, we can take tasklist for reading, or ensure that ->thread_group.prev is not
PF_EXITING, but this is nasty. Perhaps even this optimization is too ugly.
Or, perhaps we can do:
static void forget_original_parent(struct task_struct *father)
{
struct task_struct *p, *n, *reaper;
LIST_HEAD(dead_childs);
if (list_empty(&father->children))
return;
write_lock_irq(&tasklist_lock);
again:
reaper = find_new_reaper(father);
list_for_each_entry_safe(p, n, &father->children, sibling) {
p->real_parent = reaper;
if (p->parent == father) {
BUG_ON(p->ptrace);
p->parent = p->real_parent;
}
reparent_thread(father, p, &dead_childs);
}
smp_mb(); /* do_exit() does mb() after exit_signals() */
if (unlikely(reaper->flags & PF_EXITING)) {
father = reaper;
goto again;
}
write_unlock_irq(&tasklist_lock);
BUG_ON(!list_empty(&father->children));
list_for_each_entry_safe(p, n, &dead_childs, sibling) {
list_del_init(&p->sibling);
release_task(p);
}
}but this needs a fat comment at least. I am not sure this worth the trouble.
Oleg.