exec: Cleanup exec from a non thread group leader.
Quoted 8 lines · Eric W. Biederman
And for good measure we set the thread group leaders
exit_signalto -1 so it will self reap. We are actually past the point where that matters but it can't hurt, and it might help someday.
...leader->exit_state = EXIT_DEAD;+
leader->exit_signal= -1;
I disagree. The leader is already practically reaped, it is EXIT_DEAD. I think this change will confuse the reader who will try to understand why do we need this subtle assignment.
Quoted 11 lines
void
switch_exec_pids(task_t *leader, task_t *thread){(quoted diff) +0
-9- __detach_pid(leader, PIDTYPE_PID); - __detach_pid(leader, PIDTYPE_TGID); - __detach_pid(leader, PIDTYPE_PGID); - __detach_pid(leader, PIDTYPE_SID); - - __detach_pid(thread, PIDTYPE_PID); - __detach_pid(thread, PIDTYPE_TGID); - - leader->pid = leader->tgid = thread->pid;
Review
Quoted 10 lines
(quoted diff) +3
-6- thread->pid = thread->tgid; - - attach_pid(thread, PIDTYPE_PID, thread->pid); - attach_pid(thread, PIDTYPE_TGID, thread->tgid); + detach_pid(thread, PIDTYPE_PID); + thread->pid = leader->pid; + attach_pid(thread, PIDTYPE_PID, thread->pid); attach_pid(thread, PIDTYPE_PGID, thread->signal->pgrp); - attach_pid(thread, PIDTYPE_SID, thread->signal->session); - list_add_tail(&thread->tasks, &init_task.tasks);
The last deletion is wrong, I beleive.
Review
Quoted 11 lines
(quoted diff) +5
-4+ attach_pid(thread, PIDTYPE_SID, thread->signal->session); - attach_pid(leader, PIDTYPE_PID, leader->pid); - attach_pid(leader, PIDTYPE_TGID, leader->tgid); - attach_pid(leader, PIDTYPE_PGID, leader->signal->pgrp); - attach_pid(leader, PIDTYPE_SID, leader->signal->session); + detach_pid(leader, PIDTYPE_PID); + detach_pid(leader, PIDTYPE_TGID); + detach_pid(leader, PIDTYPE_PGID); + detach_pid(leader, PIDTYPE_SID); }
I think most of detach_pid()s could be replaced with __detach_pid(), this will save unneccesary pid_hash scanning
Oleg.
Quoted 6 lines · Oleg Nesterov
Quoted 2 lines · Eric W. Biederman
-
list_add_tail(&thread->tasks, &init_task.tasks);The last deletion is wrong, I beleive.
Just to clarify, it looks like we can kill this line because de_thread() also does list_add_tail(current, &init_task.tasks).
But please note that it (and probably __ptrace_link() above) does list_del(current->task) first, and current->task may have very stale values after old leader called dup_task_struct(). SET_LINKS() in copy_process() does nothing with ->tasks in a CLONE_THREAD case.
Oleg.
Quoted 8 lines · Oleg Nesterov
Quoted 6 lines · Oleg Nesterov
Quoted 2 lines · Eric W. Biederman
-
list_add_tail(&thread->tasks, &init_task.tasks);The last deletion is wrong, I beleive.
Quoted 8 lines
Just to clarify, it looks like we can kill this line because
de_thread()also doeslist_add_tail(current, &init_task.tasks).But please note that it (and probably
__ptrace_link()above) doeslist_del(current->task)first, andcurrent->taskmay have very stale values after old leader calleddup_task_struct().SET_LINKS()incopy_process()does nothing with ->tasks in aCLONE_THREADcase.
Good point in that instance we need to remove the list_del as well.
As for the other stale data that bears looking at.
Eric
Quoted 14 lines · Eric W. Biederman
Quoted 11 lines · Oleg Nesterov
Quoted 6 lines · Oleg Nesterov
Quoted 2 lines · Eric W. Biederman
-
list_add_tail(&thread->tasks, &init_task.tasks);The last deletion is wrong, I beleive.
Just to clarify, it looks like we can kill this line because
de_thread()also doeslist_add_tail(current, &init_task.tasks).
Quoted 8 lines
Quoted 5 lines
But please note that it (and probably
__ptrace_link()above) doeslist_del(current->task)first, andcurrent->taskmay have very stale values after old leader calleddup_task_struct().SET_LINKS()incopy_process()does nothing with ->tasks in aCLONE_THREADcase.Good point in that instance we need to remove the
list_delas well.
We can't just remove this list_del, note __ptrace_link() above. So if we remove list_add from switch_exec_pids() (like you did in your patch) we should also place list_add before ptrace_link() in de_thread(), otherwise I beleive it is a bug.
I agree, we should cleanup this. I just noticed that I forgot to add you on CC: list while sending this patch:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113862839924746
Btw, I don't understand why __ptrace_link() use REMOVE_LINKS/SET_LINKS instead of remove_parent/add_parent.
Oleg.
Quoted 4 lines · Oleg Nesterov
We can't just remove this
list_del, note__ptrace_link()above. So if we removelist_addfromswitch_exec_pids()(like you did in your patch) we should also placelist_addbeforeptrace_link()inde_thread(), otherwise I beleive it is a bug.
Ok. I see it now. The REMOVE_LINKS/SET_LINKS deep in __ptrace_link() touching the task list is sneaky.
Quoted 7 lines
I agree, we should cleanup this. I just noticed that I forgot to add you on CC: list while sending this patch:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113862839924746Btw, I don't understand why
__ptrace_link()use REMOVE_LINKS/SET_LINKS instead of remove_parent/add_parent.
I see one of two possibilities.
- Either there is a magic invariant that is supposed to be preserved about always being on the task list with a parent.
(And the code in this part of exec is already broken).
- Or the code is just being inefficient.
A corollary is why is any of this code safe to run without holding the tasklist_lock?
Eric
Quoted 10 lines · Oleg Nesterov
Quoted 8 lines · Eric W. Biederman
And for good measure we set the thread group leaders
exit_signalto -1 so it will self reap. We are actually past the point where that matters but it can't hurt, and it might help someday.
...leader->exit_state = EXIT_DEAD;+
leader->exit_signal= -1;
Quoted 3 lines
I disagree. The leader is already practically reaped, it is
EXIT_DEAD. I think this change will confuse the reader who will try to understand why do we need this subtle assignment.
Six of one half dozen of the other. It doesn't matter so I don't care.
Quoted 15 lines
Quoted 15 lines
void
switch_exec_pids(task_t *leader, task_t *thread){(quoted diff) +0
-13- __detach_pid(leader, PIDTYPE_PID); - __detach_pid(leader, PIDTYPE_TGID); - __detach_pid(leader, PIDTYPE_PGID); - __detach_pid(leader, PIDTYPE_SID); - - __detach_pid(thread, PIDTYPE_PID); - __detach_pid(thread, PIDTYPE_TGID); - - leader->pid = leader->tgid = thread->pid; - thread->pid = thread->tgid; - - attach_pid(thread, PIDTYPE_PID, thread->pid); - attach_pid(thread, PIDTYPE_TGID, thread->tgid);
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +3
-2+ detach_pid(thread, PIDTYPE_PID); + thread->pid = leader->pid; + attach_pid(thread, PIDTYPE_PID, thread->pid); attach_pid(thread, PIDTYPE_PGID, thread->signal->pgrp); - attach_pid(thread, PIDTYPE_SID, thread->signal->session); - list_add_tail(&thread->tasks, &init_task.tasks);The last deletion is wrong, I beleive.
list_add_tail is duplicate code. It is already present in the caller. So it is noise and confusing to leave it here.
But you already noted that in the following email.
Quoted 14 lines
Review
Quoted 11 lines
(quoted diff) +5
-4+ attach_pid(thread, PIDTYPE_SID, thread->signal->session); - attach_pid(leader, PIDTYPE_PID, leader->pid); - attach_pid(leader, PIDTYPE_TGID, leader->tgid); - attach_pid(leader, PIDTYPE_PGID, leader->signal->pgrp); - attach_pid(leader, PIDTYPE_SID, leader->signal->session); + detach_pid(leader, PIDTYPE_PID); + detach_pid(leader, PIDTYPE_TGID); + detach_pid(leader, PIDTYPE_PGID); + detach_pid(leader, PIDTYPE_SID); }I think most of detach_pid()s could be replaced with
__detach_pid(), this will save unneccesarypid_hashscanning
Actually 90% of the point was to remove the need for __detach_pid. But you are right __detach_pid would be safe and we know that because of the ordering. At the same time because we are not the last reference the code will never do that.
I need to relook at this. To not conflict with your code some of the detach_pids need to be removed so we don't unhash things twice.
Eric