Skip to contentSkip to conversation

ptrace: reintroduce __ptrace_detach() as a callee of ptrace_exit()

PATCH In review Oleg Nesterov 2 participants 6 messages Jan 29, 2009 – Feb 9, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 3/4 ptrace: reintroduce __ptrace_detach() as a callee of ptrace_exit()

No functional changes, preparation for the next patch.

Move the "should we release this child" logic into the separate handler, __ptrace_detach().

kernel/exit.c renamed from kernel/exit.c~3_REINTRODUCE_DETACH +34 -29
@@ -737,6 +737,39 @@ static int ignoring_children(struct sigh
 		sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT;
 }
 
+/* Returns nonzero if the tracee should be released. */
+int __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
+{
+	__ptrace_unlink(p);
+
+	if (p->exit_state != EXIT_ZOMBIE)
+		return 0;
+	/*
+	 * If it's a zombie, our attachedness prevented normal
+	 * parent notification or self-reaping.  Do notification
+	 * now if it would have happened earlier.  If it should
+	 * reap itself, add it to the @dead list.  We can't call
+	 * release_task() here because we already hold tasklist_lock.
+	 *
+	 * If it's our own child, there is no notification to do.
+	 * But if our normal children self-reap, then this child
+	 * was prevented by ptrace and we must reap it now.
+	 */
+	if (!task_detached(p) && thread_group_empty(p)) {
+		if (!same_thread_group(p->real_parent, tracer))
+			do_notify_parent(p, p->exit_signal);
+		else if (ignoring_children(tracer->sighand))
+			p->exit_signal = -1;
+	}
+
+	if (!task_detached(p))
+		return 0;
+
+	/* Mark it as in the process of being reaped. */
+	p->exit_state = EXIT_DEAD;
+	return 1;
+}
+
 /*
  * Detach all tasks we were using ptrace on.
  * Any that need to be release_task'd are put on the @dead list.
@@ -748,36 +781,8 @@ static void ptrace_exit(struct task_stru
 	struct task_struct *p, *n;
 
 	list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
-		__ptrace_unlink(p);
-
-		if (p->exit_state != EXIT_ZOMBIE)
-			continue;
-
-		/*
-		 * If it's a zombie, our attachedness prevented normal
-		 * parent notification or self-reaping.  Do notification
-		 * now if it would have happened earlier.  If it should
-		 * reap itself, add it to the @dead list.  We can't call
-		 * release_task() here because we already hold tasklist_lock.
-		 *
-		 * If it's our own child, there is no notification to do.
-		 * But if our normal children self-reap, then this child
-		 * was prevented by ptrace and we must reap it now.
-		 */
-		if (!task_detached(p) && thread_group_empty(p)) {
-			if (!same_thread_group(p->real_parent, parent))
-				do_notify_parent(p, p->exit_signal);
-			else if (ignoring_children(parent->sighand))
-				p->exit_signal = -1;
-		}
-
-		if (task_detached(p)) {
-			/*
-			 * Mark it as in the process of being reaped.
-			 */
-			p->exit_state = EXIT_DEAD;
+		if (__ptrace_detach(parent, p))
 			list_add(&p->ptrace_entry, dead);
-		}
 	}
 }
 
#1 lore raw
Quoted 4 lines

No functional changes, preparation for the next patch.

Move the "should we release this child" logic into the separate handler, __ptrace_detach().

My inclination is to use bool in new code for true/false return values, but I don't really care.

Please canonicalize the comment formatting for your new comments.

The preserved comment no longer makes sense, there is no "dead list" in that function. Make it a coherent comment at the top that explains the return value.

Given its content, this function now better belongs in ptrace.c, I think.

Thanks,
Roland

=====

/*
* Called with tasklist_lock held for writing.
* Unlink a traced task, and clean it up if it was a traced zombie.
* Return true if it needs to be reaped with release_task().
* (We can't call release_task() here because we already hold tasklist_lock.) *
* If it's a zombie, our attachedness prevented normal parent notification
* or self-reaping. Do notification now if it would have happened earlier.
* If it should reap itself, return true.
*
* If it's our own child, there is no notification to do.
* But if our normal children self-reap, then this child
* was prevented by ptrace and we must reap it now.
*/
bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)

{
	__ptrace_unlink(p);

	if (p->exit_state == EXIT_ZOMBIE) {
		if (!task_detached(p) && thread_group_empty(p)) {
			if (!same_thread_group(p->real_parent, tracer))
				do_notify_parent(p, p->exit_signal);
			else if (ignoring_children(tracer->sighand))
				p->exit_signal = -1;
		}
		if (task_detached(p)) {
			/*
			 * Mark it as in the process of being reaped.
			 */
			p->exit_state = EXIT_DEAD;
			return true;
		}
	}

	return false;
}
#2 lore raw
Quoted 6 lines · Roland McGrath
Quoted 5 lines

No functional changes, preparation for the next patch.

Move the "should we release this child" logic into the separate handler, __ptrace_detach().

Quoted 8 lines

My inclination is to use bool in new code for true/false return values, but I don't really care.

Please canonicalize the comment formatting for your new comments.

The preserved comment no longer makes sense, there is no "dead list" in that function. Make it a coherent comment at the top that explains the return value.

OK, I'll send the cleanup patch.

Quoted 1 line

Given its content, this function now better belongs in ptrace.c, I think.

I don't completely agree... This helper imho has nothing to do with ptracing, except it does __ptrace_unlink(). But OK, I will move it if you prefer. In that case we should export task_detached().

Oleg.

#3 lore raw
Quoted 5 lines
Quoted 1 line

Given its content, this function now better belongs in ptrace.c, I think.

I don't completely agree... This helper imho has nothing to do with ptracing, except it does __ptrace_unlink(). But OK, I will move it if you prefer.

Obviously where it goes is not a big deal. But I think it's clear that it has everything to do with ptrace and nothing to do with anything else. It resolves a situation that can only arise because of ptrace magic.

Quoted 1 line

In that case we should export task_detached().

Or just recognize that this trivial wrapper around == -1 has little value two lines away from a place where = -1 is done explicitly. Really, the "abstraction" is more confusing than not in this function, IMHO.

Thanks,
Roland

#4 lore raw
Quoted 10 lines · Roland McGrath
Quoted 6 lines
Quoted 2 lines

Given its content, this function now better belongs in ptrace.c, I think.

I don't completely agree... This helper imho has nothing to do with ptracing, except it does __ptrace_unlink(). But OK, I will move it if you prefer.

Obviously where it goes is not a big deal. But I think it's clear that it has everything to do with ptrace and nothing to do with anything else. It resolves a situation that can only arise because of ptrace magic.

OK, OK, I will move it.

Quoted 5 lines
Quoted 1 line

In that case we should export task_detached().

Or just recognize that this trivial wrapper around == -1 has little value two lines away from a place where = -1 is done explicitly. Really, the "abstraction" is more confusing than not in this function, IMHO.

Well, yes. The only problem it is not easy to grep for this check without a helper.

(And I still hope we can change the rules sometimes, I think there
is no good reason to have task_detached() or EXIT_DEAD tasks on ->children list. But this is offtopic.)

Oleg.

#5 lore raw
Quoted 3 lines

(And I still hope we can change the rules sometimes, I think there
is no good reason to have task_detached() or EXIT_DEAD tasks on ->children list. But this is offtopic.)

Agreed on both counts. :-)

Thanks,
Roland