Skip to content

Commit 18c830d

Browse files
oleg-nesterovtorvalds
authored andcommitted
kernel/fork.c:copy_process(): consolidate the lockless CLONE_THREAD checks
copy_process() does a lot of "chaotic" initializations and checks CLONE_THREAD twice before it takes tasklist. In particular it sets "p->group_leader = p" and then changes it again under tasklist if !thread_group_leader(p). This looks a bit confusing, lets create a single "if (CLONE_THREAD)" block which initializes ->exit_signal, ->group_leader, and ->tgid. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Michal Hocko <mhocko@suse.cz> Cc: Pavel Emelyanov <xemul@parallels.com> Cc: Sergey Dyasly <dserrg@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8190773 commit 18c830d

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

‎kernel/fork.c‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,11 +1360,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
13601360
goto bad_fork_cleanup_io;
13611361
}
13621362

1363-
p->pid = pid_nr(pid);
1364-
p->tgid = p->pid;
1365-
if (clone_flags & CLONE_THREAD)
1366-
p->tgid = current->tgid;
1367-
13681363
p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
13691364
/*
13701365
* Clear TID on mm_release()?
@@ -1400,12 +1395,19 @@ static struct task_struct *copy_process(unsigned long clone_flags,
14001395
clear_all_latency_tracing(p);
14011396

14021397
/* ok, now we should be set up.. */
1403-
if (clone_flags & CLONE_THREAD)
1398+
p->pid = pid_nr(pid);
1399+
if (clone_flags & CLONE_THREAD) {
14041400
p->exit_signal = -1;
1405-
else if (clone_flags & CLONE_PARENT)
1406-
p->exit_signal = current->group_leader->exit_signal;
1407-
else
1408-
p->exit_signal = (clone_flags & CSIGNAL);
1401+
p->group_leader = current->group_leader;
1402+
p->tgid = current->tgid;
1403+
} else {
1404+
if (clone_flags & CLONE_PARENT)
1405+
p->exit_signal = current->group_leader->exit_signal;
1406+
else
1407+
p->exit_signal = (clone_flags & CSIGNAL);
1408+
p->group_leader = p;
1409+
p->tgid = p->pid;
1410+
}
14091411

14101412
p->pdeath_signal = 0;
14111413
p->exit_state = 0;
@@ -1414,15 +1416,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,
14141416
p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
14151417
p->dirty_paused_when = 0;
14161418

1417-
/*
1418-
* Ok, make it visible to the rest of the system.
1419-
* We dont wake it up yet.
1420-
*/
1421-
p->group_leader = p;
14221419
INIT_LIST_HEAD(&p->thread_group);
14231420
p->task_works = NULL;
14241421

1425-
/* Need tasklist lock for parent etc handling! */
1422+
/*
1423+
* Make it visible to the rest of the system, but dont wake it up yet.
1424+
* Need tasklist lock for parent etc handling!
1425+
*/
14261426
write_lock_irq(&tasklist_lock);
14271427

14281428
/* CLONE_PARENT re-uses the old parent */
@@ -1476,7 +1476,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
14761476
current->signal->nr_threads++;
14771477
atomic_inc(&current->signal->live);
14781478
atomic_inc(&current->signal->sigcnt);
1479-
p->group_leader = current->group_leader;
14801479
list_add_tail_rcu(&p->thread_group,
14811480
&p->group_leader->thread_group);
14821481
}

0 commit comments

Comments
 (0)