Skip to content

Commit 749860c

Browse files
Snorchebiederm
authored andcommitted
prctl: propagate has_child_subreaper flag to every descendant
If process forks some children when it has is_child_subreaper flag enabled they will inherit has_child_subreaper flag - first group, when is_child_subreaper is disabled forked children will not inherit it - second group. So child-subreaper does not reparent all his descendants when their parents die. Having these two differently behaving groups can lead to confusion. Also it is a problem for CRIU, as when we restore process tree we need to somehow determine which descendants belong to which group and much harder - to put them exactly to these group. To simplify these we can add a propagation of has_child_subreaper flag on PR_SET_CHILD_SUBREAPER, walking all descendants of child- subreaper to setup has_child_subreaper flag. In common cases when process like systemd first sets itself to be a child-subreaper and only after that forks its services, we will have zero-length list of descendants to walk. Testing with binary subtree of 2^15 processes prctl took < 0.007 sec and has shown close to linear dependency(~0.2 * n * usec) on lower numbers of processes. Moreover, I doubt someone intentionaly pre-forks the children whitch should reparent to init before becoming subreaper, because some our ancestor migh have had is_child_subreaper flag while forking our sub-tree and our childs will all inherit has_child_subreaper flag, and we have no way to influence it. And only way to check if we have no has_child_subreaper flag is to create some childs, kill them and see where they will reparent to. Using walk_process_tree helper to walk subtree, thanks to Oleg! Timing seems to be the same. Optimize: a) When descendant already has has_child_subreaper flag all his subtree has it too already. * for a) to be true need to move has_child_subreaper inheritance under the same tasklist_lock with adding task to its ->real_parent->children as without it process can inherit zero has_child_subreaper, then we set 1 to it's parent flag, check that parent has no more children, and only after child with wrong flag is added to the tree. * Also make these inheritance more clear by using real_parent instead of current, as on clone(CLONE_PARENT) if current has is_child_subreaper and real_parent has no is_child_subreaper or has_child_subreaper, child will have has_child_subreaper flag set without actually having a subreaper in it's ancestors. b) When some descendant is child_reaper, it's subtree is in different pidns from us(original child-subreaper) and processes from other pidns will never reparent to us. So we can skip their(a,b) subtree from walk. v2: switch to walk_process_tree() general helper, move has_child_subreaper inheritance v3: remove csr_descendant leftover, change current to real_parent in has_child_subreaper inheritance v4: small commit message fix Fixes: ebec18a ("prctl: add PR_{SET,GET}_CHILD_SUBREAPER to allow simple process supervision") Signed-off-by: Pavel Tikhomirov <[email protected]> Reviewed-by: Oleg Nesterov <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent 0f1b92c commit 749860c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

kernel/fork.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,9 +1367,6 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
13671367
sig->oom_score_adj = current->signal->oom_score_adj;
13681368
sig->oom_score_adj_min = current->signal->oom_score_adj_min;
13691369

1370-
sig->has_child_subreaper = current->signal->has_child_subreaper ||
1371-
current->signal->is_child_subreaper;
1372-
13731370
mutex_init(&sig->cred_guard_mutex);
13741371

13751372
return 0;
@@ -1800,6 +1797,13 @@ static __latent_entropy struct task_struct *copy_process(
18001797

18011798
p->signal->leader_pid = pid;
18021799
p->signal->tty = tty_kref_get(current->signal->tty);
1800+
/*
1801+
* Inherit has_child_subreaper flag under the same
1802+
* tasklist_lock with adding child to the process tree
1803+
* for propagate_has_child_subreaper optimization.
1804+
*/
1805+
p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper ||
1806+
p->real_parent->signal->is_child_subreaper;
18031807
list_add_tail(&p->sibling, &p->real_parent->children);
18041808
list_add_tail_rcu(&p->tasks, &init_task.tasks);
18051809
attach_pid(p, PIDTYPE_PGID);

kernel/sys.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,24 @@ static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
20632063
}
20642064
#endif
20652065

2066+
static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2067+
{
2068+
/*
2069+
* If task has has_child_subreaper - all its decendants
2070+
* already have these flag too and new decendants will
2071+
* inherit it on fork, skip them.
2072+
*
2073+
* If we've found child_reaper - skip descendants in
2074+
* it's subtree as they will never get out pidns.
2075+
*/
2076+
if (p->signal->has_child_subreaper ||
2077+
is_child_reaper(task_pid(p)))
2078+
return 0;
2079+
2080+
p->signal->has_child_subreaper = 1;
2081+
return 1;
2082+
}
2083+
20662084
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
20672085
unsigned long, arg4, unsigned long, arg5)
20682086
{
@@ -2214,6 +2232,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
22142232
break;
22152233
case PR_SET_CHILD_SUBREAPER:
22162234
me->signal->is_child_subreaper = !!arg2;
2235+
if (!arg2)
2236+
break;
2237+
2238+
walk_process_tree(me, propagate_has_child_subreaper, NULL);
22172239
break;
22182240
case PR_GET_CHILD_SUBREAPER:
22192241
error = put_user(me->signal->is_child_subreaper,

0 commit comments

Comments
 (0)