c/r: fix race of prepare_descendant() with an ongoing fork()
Subsystems (3)
PATCH 1/1 c/r: fix race of prepare_descendant() with an ongoing fork()
From: Oren Laadan <[email protected]>
If prepare_descendants() is walking a tree and one of the tasks is forking, one of two bads can happen. If the child doesn't inherit the ->ctx, it breaks the assumption that the entire subtree is prepared. If the child inherits the ->ctx, it will have one without having taken a reference.
This patch closed this race by explicitly getting and referencing the ->ctx for a child process should the parent have one, atomically under the tasklist_lock.
--- kernel/fork.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)
kernel/fork.c +8 -3
@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/checkpoint.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -1148,9 +1149,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->pi_state_list); p->pi_state_cache =NULL ;#endif -#ifdef CONFIG_CHECKPOINT - p->checkpoint_ctx =NULL ; -#endif /* * sigaltstack should be cleared when sharing the same VM */
@@ -1188,6 +1186,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,/* Need tasklist lock for parent etc handling! */ write_lock_irq (&tasklist_lock); +#ifdef CONFIG_CHECKPOINT +/* If parent is restarting, child should be too */ +if (unlikely (current->checkpoint_ctx)) { + p->checkpoint_ctx = current->checkpoint_ctx; +ckpt_ctx_get (p->checkpoint_ctx); + } +#endif /* * The task hasn't been attached yet, so its cpus_allowed mask will * not be changed, nor will its assigned CPU.
Signature · 1 line
1.6.0.4
A message that is not in the archive (sent off-list or not yet received): 1253652463-11529-1-git-send-email-orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org
Quoted 40 lines · Oren Laadan
From: Oren Laadan <[email protected]>
If
prepare_descendants()is walking a tree and one of the tasks is forking, one of two bads can happen. If the child doesn't inherit the ->ctx, it breaks the assumption that the entire subtree is prepared. If the child inherits the ->ctx, it will have one without having taken a reference.This patch closed this race by explicitly getting and referencing the ->ctx for a child process should the parent have one, atomically under the
tasklist_lock.Signed-off-by Oren Laadan <[email protected]>--- kernel/fork.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)kernel/fork.c +1
-3@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/checkpoint.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -1148,9 +1149,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->pi_state_list); p->pi_state_cache =NULL ;#endif -#ifdef CONFIG_CHECKPOINT - p->checkpoint_ctx =NULL ; -#endif /* * sigaltstack should be cleared when sharing the same VM */
kernel/fork.c static struct task_struct *copy_process(unsigned long clone_flags, · line 1192
Quoted 7 lines
kernel/fork.c +4
-0@@ -1188,6 +1186,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,/* Need tasklist lock for parent etc handling! */ write_lock_irq (&tasklist_lock); +#ifdef CONFIG_CHECKPOINT +/* If parent is restarting, child should be too */ +if (unlikely (current->checkpoint_ctx)) { + p->checkpoint_ctx = current->checkpoint_ctx;
Won't break anything, but technically p->checkpoint_ctx will already be copied from current->checkpoint_ctx, so only the ckpt_ctx_get() is necessary, so this could really read
if (p->checkpoint_ctx) ckpt_ctx_get(p->checkpoint_ctx);
Right?
Quoted 13 lines
kernel/fork.c +3
-0+ ckpt_ctx_get(p->checkpoint_ctx); + } +#endif /* * The task hasn't been attached yet, so its cpus_allowed mask will * not be changed, nor will its assigned CPU. Signature · 6 lines
1.6.0.4 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers
A message that is not in the archive (sent off-list or not yet received): 20090922213858.GA30048-r/[email protected]
Quoted 53 lines · Serge E. Hallyn
Quoted 40 lines · Oren Laadan
From: Oren Laadan <[email protected]>
If
prepare_descendants()is walking a tree and one of the tasks is forking, one of two bads can happen. If the child doesn't inherit the ->ctx, it breaks the assumption that the entire subtree is prepared. If the child inherits the ->ctx, it will have one without having taken a reference.This patch closed this race by explicitly getting and referencing the ->ctx for a child process should the parent have one, atomically under the
tasklist_lock.Signed-off-by Oren Laadan <[email protected]>--- kernel/fork.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)kernel/fork.c +1
-3@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/checkpoint.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -1148,9 +1149,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->pi_state_list); p->pi_state_cache =NULL ;#endif -#ifdef CONFIG_CHECKPOINT - p->checkpoint_ctx =NULL ; -#endif /* * sigaltstack should be cleared when sharing the same VM */ kernel/fork.c static struct task_struct *copy_process(unsigned long clone_flags, · line 1192
Quoted 7 lines
kernel/fork.c +4
-0@@ -1188,6 +1186,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,/* Need tasklist lock for parent etc handling! */ write_lock_irq (&tasklist_lock); +#ifdef CONFIG_CHECKPOINT +/* If parent is restarting, child should be too */ +if (unlikely (current->checkpoint_ctx)) { + p->checkpoint_ctx = current->checkpoint_ctx;Won't break anything, but technically
p->checkpoint_ctxwill already be copied fromcurrent->checkpoint_ctx, so only theckpt_ctx_get()is necessary, so this could really read
Quoted 4 lines
if (p->checkpoint_ctx) ckpt_ctx_get(p->checkpoint_ctx);Right?
BTW since all I'm doing is nit-picking, I obviously agree with the patch on the whole :)
There is no way for the task to be forked into a traced state (without the parent being traced) right? And, is the fact that ctx->nr_total may end up less than the total number of active tasks a problem at all?
thanks,
-serge
A message that is not in the archive (sent off-list or not yet received): 20090922214619.GA30215-r/[email protected]
Quoted 56 lines · Serge E. Hallyn
Quoted 55 lines · Serge E. Hallyn
Quoted 40 lines · Oren Laadan
From: Oren Laadan <[email protected]>
If
prepare_descendants()is walking a tree and one of the tasks is forking, one of two bads can happen. If the child doesn't inherit the ->ctx, it breaks the assumption that the entire subtree is prepared. If the child inherits the ->ctx, it will have one without having taken a reference.This patch closed this race by explicitly getting and referencing the ->ctx for a child process should the parent have one, atomically under the
tasklist_lock.Signed-off-by Oren Laadan <[email protected]>--- kernel/fork.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)kernel/fork.c +1
-3@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/checkpoint.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -1148,9 +1149,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->pi_state_list); p->pi_state_cache =NULL ;#endif -#ifdef CONFIG_CHECKPOINT - p->checkpoint_ctx =NULL ; -#endif /* * sigaltstack should be cleared when sharing the same VM */ kernel/fork.c static struct task_struct *copy_process(unsigned long clone_flags, · line 1192
Quoted 7 lines
kernel/fork.c +4
-0@@ -1188,6 +1186,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,/* Need tasklist lock for parent etc handling! */ write_lock_irq (&tasklist_lock); +#ifdef CONFIG_CHECKPOINT +/* If parent is restarting, child should be too */ +if (unlikely (current->checkpoint_ctx)) { + p->checkpoint_ctx = current->checkpoint_ctx;Won't break anything, but technically
p->checkpoint_ctxwill already be copied fromcurrent->checkpoint_ctx, so only theckpt_ctx_get()is necessary, so this could really readif (p->checkpoint_ctx) ckpt_ctx_get(p->checkpoint_ctx);
Quoted 7 lines
Quoted 1 line
Right?
BTW since all I'm doing is nit-picking, I obviously agree with the patch on the whole :)
There is no way for the task to be forked into a traced state (without the parent being traced) right? And, is the fact that
Not that I'm aware of.
Quoted 2 lines
ctx->nr_totalmay end up less than the total number of active tasks a problem at all?
That's ok. A restart will fail if there are not enough tasks, and will not-succeed (hang until interrupted) if there are "too many" tasks.
The above is a protection against malicious use of sys_restart(), not a fix to a correct use through (bug-less!) restart.c :p
Oren.
Quoted 3 lines
thanks,
-serge
Quoted 53 lines · Serge E. Hallyn
Quoted 40 lines · Oren Laadan
From: Oren Laadan <[email protected]>
If
prepare_descendants()is walking a tree and one of the tasks is forking, one of two bads can happen. If the child doesn't inherit the ->ctx, it breaks the assumption that the entire subtree is prepared. If the child inherits the ->ctx, it will have one without having taken a reference.This patch closed this race by explicitly getting and referencing the ->ctx for a child process should the parent have one, atomically under the
tasklist_lock.Signed-off-by Oren Laadan <[email protected]>--- kernel/fork.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)kernel/fork.c +1
-3@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/checkpoint.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -1148,9 +1149,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->pi_state_list); p->pi_state_cache =NULL ;#endif -#ifdef CONFIG_CHECKPOINT - p->checkpoint_ctx =NULL ; -#endif /* * sigaltstack should be cleared when sharing the same VM */ kernel/fork.c static struct task_struct *copy_process(unsigned long clone_flags, · line 1192
Quoted 7 lines
kernel/fork.c +4
-0@@ -1188,6 +1186,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,/* Need tasklist lock for parent etc handling! */ write_lock_irq (&tasklist_lock); +#ifdef CONFIG_CHECKPOINT +/* If parent is restarting, child should be too */ +if (unlikely (current->checkpoint_ctx)) { + p->checkpoint_ctx = current->checkpoint_ctx;Won't break anything, but technically
p->checkpoint_ctxwill already be copied fromcurrent->checkpoint_ctx, so only theckpt_ctx_get()is necessary, so this could really read
Quoted 4 lines
if (p->checkpoint_ctx) ckpt_ctx_get(p->checkpoint_ctx);Right?
That's what I initially thought. However, it _is_ possible that the following occurs:
1) Task A starts a fork an does some of copy_process(): so @p is NULL, but copy_process() isn't complete
2) An ancestor of task A calls prepare_descendants(), which then places a valid checkpoint_ctx on A.
That is why I placed this test-and-set snippet while holding tasklist_lock (write): it ensure that the child has whatever the parent has, because prepare_descendants() uses the same lock.
Oren.
Quoted 14 lines
Quoted 14 lines
kernel/fork.c +3
-0+ ckpt_ctx_get(p->checkpoint_ctx); + } +#endif /* * The task hasn't been attached yet, so its cpus_allowed mask will * not be changed, nor will its assigned CPU. Signature · 6 lines
1.6.0.4 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers
A message that is not in the archive (sent off-list or not yet received): [email protected]
Serge E. Hallyn
Acked-byQuoted 61 lines · Oren Laadan
Quoted 53 lines · Serge E. Hallyn
Quoted 40 lines · Oren Laadan
From: Oren Laadan <[email protected]>
If
prepare_descendants()is walking a tree and one of the tasks is forking, one of two bads can happen. If the child doesn't inherit the ->ctx, it breaks the assumption that the entire subtree is prepared. If the child inherits the ->ctx, it will have one without having taken a reference.This patch closed this race by explicitly getting and referencing the ->ctx for a child process should the parent have one, atomically under the
tasklist_lock.Signed-off-by Oren Laadan <[email protected]>--- kernel/fork.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)kernel/fork.c +1
-3@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/checkpoint.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -1148,9 +1149,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,INIT_LIST_HEAD (&p->pi_state_list); p->pi_state_cache =NULL ;#endif -#ifdef CONFIG_CHECKPOINT - p->checkpoint_ctx =NULL ; -#endif /* * sigaltstack should be cleared when sharing the same VM */ kernel/fork.c static struct task_struct *copy_process(unsigned long clone_flags, · line 1192
Quoted 7 lines
kernel/fork.c +4
-0@@ -1188,6 +1186,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,/* Need tasklist lock for parent etc handling! */ write_lock_irq (&tasklist_lock); +#ifdef CONFIG_CHECKPOINT +/* If parent is restarting, child should be too */ +if (unlikely (current->checkpoint_ctx)) { + p->checkpoint_ctx = current->checkpoint_ctx;Won't break anything, but technically
p->checkpoint_ctxwill already be copied fromcurrent->checkpoint_ctx, so only theckpt_ctx_get()is necessary, so this could really readQuoted 4 lines
if (p->checkpoint_ctx) ckpt_ctx_get(p->checkpoint_ctx);Right?
Quoted 8 lines
That's what I initially thought. However, it _is_ possible that the following occurs:
1) Task A starts a fork an does some of
copy_process(): so @p is NULL, butcopy_process()isn't complete2) An ancestor of task A calls
prepare_descendants(), which then places a validcheckpoint_ctxon A.
Huh. Guess so. Thanks.
Quoted 21 lines
That is why I placed this test-and-set snippet while holding
tasklist_lock(write): it ensure that the child has whatever the parent has, becauseprepare_descendants()uses the same lock.Oren.
Quoted 14 lines
Quoted 14 lines
kernel/fork.c +3
-0+ ckpt_ctx_get(p->checkpoint_ctx); + } +#endif /* * The task hasn't been attached yet, so its cpus_allowed mask will * not be changed, nor will its assigned CPU. Signature · 6 lines
1.6.0.4 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers