public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 4/4] reparent/untrace: do nothing if no childs/tracees
@ 2009-02-11 21:12 Oleg Nesterov
  2009-02-11 21:14 ` Oleg Nesterov
  0 siblings, 1 reply; 2+ messages in thread
From: Oleg Nesterov @ 2009-02-11 21:12 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Eric W. Biederman, Metzger, Markus T, Roland McGrath,
	linux-kernel

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.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>

--- 6.29-rc3/kernel/exit.c~4_TASKLIST	2009-02-11 07:20:54.000000000 +0100
+++ 6.29-rc3/kernel/exit.c	2009-02-11 21:25:35.000000000 +0100
@@ -803,6 +803,16 @@ static void forget_original_parent(struc
 	struct 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);
 
--- 6.29-rc3/kernel/ptrace.c~4_TASKLIST	2009-02-11 04:04:17.000000000 +0100
+++ 6.29-rc3/kernel/ptrace.c	2009-02-11 08:27:41.000000000 +0100
@@ -323,6 +323,9 @@ void exit_ptrace(struct task_struct *tra
 	struct 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))


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 4/4] reparent/untrace: do nothing if no childs/tracees
  2009-02-11 21:12 [PATCH 4/4] reparent/untrace: do nothing if no childs/tracees Oleg Nesterov
@ 2009-02-11 21:14 ` Oleg Nesterov
  0 siblings, 0 replies; 2+ messages in thread
From: Oleg Nesterov @ 2009-02-11 21:14 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Eric W. Biederman, Metzger, Markus T, Roland McGrath,
	linux-kernel

On 02/11, Oleg Nesterov wrote:
>
> 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.

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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-02-11 21:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-11 21:12 [PATCH 4/4] reparent/untrace: do nothing if no childs/tracees Oleg Nesterov
2009-02-11 21:14 ` Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox