userfaultfd: use RCU to free the task struct when fork fails
Subsystems (4)
cover userfaultfd: use RCU to free the task struct when fork fails
Hello,
this fixes a race condition between memcg and UFFD_EVENT_FORK that was reproduced on aarch64 with qemu with syzkaller.
While at it I also added more WRITE_ONCE in places that shall use it (in theory) against the rcu_deferenfence issued in the in rcu_read_lock critical section.
Andrea Arcangeli (2):
userfaultfd: use RCU to free the task struct when fork fails
mm: change mm_update_next_owner() to update mm->owner with WRITE_ONCE
kernel/exit.c | 6 +++--- kernel/fork.c | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-)
PATCH 1/2 userfaultfd: use RCU to free the task struct when fork fails
MEMCG depends on the task structure not to be freed under rcu_read_lock() in get_mem_cgroup_from_mm() after it dereferences mm->owner.
An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after fork() is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to set mm->owner to NULL and to free the task struct that was pointed by mm->owner with RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.
This race condition couldn't trigger if CONFIG_MEMCG was set =n at build time.
--- kernel/fork.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)
kernel/fork.c +32 -2
@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct *mm)#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG
@@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:
@@ -1662,6 +1672,24 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +#ifdef CONFIG_MEMCG +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +} +#endif /* CONFIG_MEMCG */ + +static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +#ifdef CONFIG_MEMCG +call_rcu (&tsk->rcu, __delayed_free_task); +#else /* CONFIG_MEMCG */ +free_task (tsk); +#endif /* CONFIG_MEMCG */ +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet.
@@ -2123,8 +2151,10 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);
@@ -2155,7 +2185,7 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p); fork_out:spin_lock_irq (¤t->sighand->siglock);hlist_del_init (&delayed.node);
Quoted 94 lines · Andrea Arcangeli
MEMCG depends on the task structure not to be freed under
rcu_read_lock()inget_mem_cgroup_from_mm()after it dereferencesmm->owner.An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to setmm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.Fixes 893e26e61d04 ("userfaultfd: non-cooperative: Add fork() event")Tested-by zhong jiang <[email protected]>Reported-by [email protected]Signed-off-by Andrea Arcangeli <[email protected]>--- kernel/fork.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)kernel/fork.c +31
-1@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct *mm)#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG @@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:@@ -1662,6 +1672,24 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +#ifdef CONFIG_MEMCG +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +} +#endif /* CONFIG_MEMCG */ + +static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +#ifdef CONFIG_MEMCG +call_rcu (&tsk->rcu, __delayed_free_task); +#else /* CONFIG_MEMCG */ +free_task (tsk); +#endif /* CONFIG_MEMCG */ +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -2123,8 +2151,10 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);
kernel/fork.c static __latent_entropy struct task_struct *copy_process( · line 2188
Quoted 5 lines
kernel/fork.c +1
-1@@ -2155,7 +2185,7 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p);
Can't call_rcu(&p->rcu, delayed_put_task_struct) be used instead this?
Kirill
Quoted 94 lines · Kirill Tkhai
Quoted 93 lines · Andrea Arcangeli
MEMCG depends on the task structure not to be freed under
rcu_read_lock()inget_mem_cgroup_from_mm()after it dereferencesmm->owner.An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to setmm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.Fixes 893e26e61d04 ("userfaultfd: non-cooperative: Add fork() event")Tested-by zhong jiang <[email protected]>Reported-by [email protected]Signed-off-by Andrea Arcangeli <[email protected]>--- kernel/fork.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)kernel/fork.c +31
-1@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct *mm)#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG @@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:@@ -1662,6 +1672,24 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +#ifdef CONFIG_MEMCG +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +} +#endif /* CONFIG_MEMCG */ + +static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +#ifdef CONFIG_MEMCG +call_rcu (&tsk->rcu, __delayed_free_task); +#else /* CONFIG_MEMCG */ +free_task (tsk); +#endif /* CONFIG_MEMCG */ +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -2123,8 +2151,10 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);
Quoted 8 lines
kernel/fork.c static __latent_entropy struct task_struct *copy_process( · line 2188
Quoted 6 lines
kernel/fork.c +1
-1@@ -2155,7 +2185,7 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p);Can't
call_rcu(&p->rcu, delayed_put_task_struct)be used instead this?
I mean:
refcount_set(&tsk->usage, 2);call_rcu(&p->rcu, delayed_put_task_struct);
And:
kernel/fork.c +4 -1
@@ -715,7 +715,9 @@ static inline void put_signal_struct(struct signal_struct *sig)void __put_task_struct (struct task_struct *tsk) { -WARN_ON (!tsk->exit_state); +if (!tsk->exit_state) +/* Cleanup of copy_process() */ + goto free;WARN_ON (refcount_read (&tsk->usage));WARN_ON (tsk == current);
@@ -727,6 +729,7 @@ void __put_task_struct(struct task_struct *tsk)put_signal_struct (tsk->signal);if (!profile_handoff_task (tsk)) +free:free_task (tsk); }EXPORT_SYMBOL_GPL (__put_task_struct);
Quoted 104 lines · Kirill Tkhai
Quoted 94 lines · Kirill Tkhai
Quoted 93 lines · Andrea Arcangeli
MEMCG depends on the task structure not to be freed under
rcu_read_lock()inget_mem_cgroup_from_mm()after it dereferencesmm->owner.An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to setmm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.Fixes 893e26e61d04 ("userfaultfd: non-cooperative: Add fork() event")Tested-by zhong jiang <[email protected]>Reported-by [email protected]Signed-off-by Andrea Arcangeli <[email protected]>--- kernel/fork.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)kernel/fork.c +31
-1@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct *mm)#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG @@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:@@ -1662,6 +1672,24 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +#ifdef CONFIG_MEMCG +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +} +#endif /* CONFIG_MEMCG */ + +static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +#ifdef CONFIG_MEMCG +call_rcu (&tsk->rcu, __delayed_free_task); +#else /* CONFIG_MEMCG */ +free_task (tsk); +#endif /* CONFIG_MEMCG */ +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -2123,8 +2151,10 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);Quoted 8 lines
kernel/fork.c static __latent_entropy struct task_struct *copy_process( · line 2188
Quoted 6 lines
kernel/fork.c +1
-1@@ -2155,7 +2185,7 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p);Can't
call_rcu(&p->rcu, delayed_put_task_struct)be used instead this?
Quoted 3 lines
I mean:
refcount_set(&tsk->usage, 2);
I.e., refcount_set(&tsk->usage, 1);
Quoted 28 lines
call_rcu(&p->rcu, delayed_put_task_struct);And:
kernel/fork.c +4
-1@@ -715,7 +715,9 @@ static inline void put_signal_struct(struct signal_struct *sig)void __put_task_struct (struct task_struct *tsk) { -WARN_ON (!tsk->exit_state); +if (!tsk->exit_state) +/* Cleanup of copy_process() */ + goto free;WARN_ON (refcount_read (&tsk->usage));WARN_ON (tsk == current);@@ -727,6 +729,7 @@ void __put_task_struct(struct task_struct *tsk)put_signal_struct (tsk->signal);if (!profile_handoff_task (tsk)) +free:free_task (tsk); }EXPORT_SYMBOL_GPL (__put_task_struct);
Quoted 3 lines · Andrea Arcangeli
MEMCG depends on the task structure not to be freed under
rcu_read_lock()inget_mem_cgroup_from_mm()after it dereferencesmm->owner.
Please state the actual problem. Your cover letter mentiones a race condition. Please make it explicit in the changelog.
Quoted 6 lines
An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe.
How much more changes are we talking about? Because ...
Quoted 6 lines
This solution as opposed only adds the dependency to common code to set
mm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.
... this is adding a subtle hack that might break in the future because copy_process error paths are far from trivial and quite error prone IMHO. I am not opposed to the patch in principle but I would really like to see what kind of solutions we are comparing here.
Quoted 2 lines
This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.
All the CONFIG_MEMCG is just ugly as hell. Can we reduce that please? E.g. use if (IS_ENABLED(CONFIG_MEMCG)) where appropriate?
[...]
kernel/fork.c static void mm_init_aio(struct mm_struct *mm) · line 961
Quoted 7 lines
kernel/fork.c +7
-0+static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif
How can we ever hit this warning and what does that mean?
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 7 lines · Michal Hocko
Quoted 3 lines · Andrea Arcangeli
MEMCG depends on the task structure not to be freed under
rcu_read_lock()inget_mem_cgroup_from_mm()after it dereferencesmm->owner.Please state the actual problem. Your cover letter mentiones a race condition. Please make it explicit in the changelog.
The actual problem is the task structure is freed while get_mem_cgroup_from_mm() holds rcu_read_lock() and dereferences mm->owner.
I thought the breakage of RCU is pretty clear, but we could add a description of the race like I did in the original thread:
https://lkml.kernel.org/r/[email protected]
https://lkml.kernel.org/r/[email protected]
Quoted 8 lines
Quoted 6 lines
An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe.How much more changes are we talking about? Because ...
I haven't implemented but I can theorize. It should require a new hooking point and information being accumulated in RAM and passed from the current hooking point to the new hooking point and to hold off the delivery of such information to the uffd monitor (the fd reader), until the new hooking point is invoked. The new hooking point would need to be invoked after fork cannot fail anymore.
We already accumulate some information in RAM there, but the first delivery happens at a point where fork can still fail.
Quoted 11 lines
Quoted 6 lines
This solution as opposed only adds the dependency to common code to set
mm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.... this is adding a subtle hack that might break in the future because
copy_processerror paths are far from trivial and quite error prone IMHO. I am not opposed to the patch in principle but I would really like to see what kind of solutions we are comparing here.
The rule of clearing mm->owner and then freeing the mm->owner memory with call_rcu is already followed everywhere else. See for example mm_update_next_owner() that sets mm->owner to NULL and only then invokes put_task_struct which frees the memory pointed by the old value of mm->owner using RCU.
The "subtle hack" already happens at every exit when MEMCG=y. All the patch does is to extend the "subtle hack" to the fork failure path too which it didn't follow the rule and it didn't clear mm->owner and it just freed the task struct without waiting for a RCU grace period. In fact like pointed out by Kirill Tkhai we could reuse delayed_put_task_struct method that is already used by exit, except it does more than freeing the task structure and it relies on refcounters to be initialized so I thought the free_task -> call_rcu( free_task) conversion was simpler and more obviously safe. Sharing the other method only looked a complication that requires syncing up the refcounts.
I think the only conceptual simplification possible would be again to add a new hooking point and more buildup of information until fork cannot fail, but in implementation terms I doubt the fix will become smaller or simpler that way.
Quoted 5 lines
Quoted 2 lines
This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.All the
CONFIG_MEMCGis just ugly as hell. Can we reduce that please? E.g. use if (IS_ENABLED(CONFIG_MEMCG)) where appropriate?
There's just one place where I could use that instead of #ifdef.
Quoted 9 lines
kernel/fork.c static void mm_init_aio(struct mm_struct *mm) · line 961
Quoted 7 lines
kernel/fork.c +7
-0+static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif How can we ever hit this warning and what does that mean?
Which warning?
Quoted 13 lines · Andrea Arcangeli
Quoted 7 lines · Michal Hocko
Quoted 3 lines · Andrea Arcangeli
MEMCG depends on the task structure not to be freed under
rcu_read_lock()inget_mem_cgroup_from_mm()after it dereferencesmm->owner.Please state the actual problem. Your cover letter mentiones a race condition. Please make it explicit in the changelog.
The actual problem is the task structure is freed while
get_mem_cgroup_from_mm()holdsrcu_read_lock()and dereferencesmm->owner.
Quoted 5 lines
I thought the breakage of RCU is pretty clear, but we could add a description of the race like I did in the original thread:
https://lkml.kernel.org/r/[email protected]
https://lkml.kernel.org/r/[email protected]
Yes please. That really belongs to the changelog. You do not expect people chasing long email threads or code to figure that out, right?
Quoted 16 lines
Quoted 8 lines
Quoted 6 lines
An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe.How much more changes are we talking about? Because ...
I haven't implemented but I can theorize. It should require a new hooking point and information being accumulated in RAM and passed from the current hooking point to the new hooking point and to hold off the delivery of such information to the uffd monitor (the fd reader), until the new hooking point is invoked. The new hooking point would need to be invoked after fork cannot fail anymore.
Quoted 2 lines
We already accumulate some information in RAM there, but the first delivery happens at a point where fork can still fail.
I am sorry but this is not really clear to me. What is the problem to postpone hooking point to later and how much more data we are talking about here?
Quoted 30 lines
Quoted 11 lines
Quoted 6 lines
This solution as opposed only adds the dependency to common code to set
mm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.... this is adding a subtle hack that might break in the future because
copy_processerror paths are far from trivial and quite error prone IMHO. I am not opposed to the patch in principle but I would really like to see what kind of solutions we are comparing here.The rule of clearing
mm->ownerand then freeing themm->ownermemory withcall_rcuis already followed everywhere else. See for examplemm_update_next_owner()that setsmm->ownerto NULL and only then invokesput_task_structwhich frees the memory pointed by the old value ofmm->ownerusing RCU.The "subtle hack" already happens at every exit when MEMCG=y. All the patch does is to extend the "subtle hack" to the fork failure path too which it didn't follow the rule and it didn't clear
mm->ownerand it just freed the task struct without waiting for a RCU grace period. In fact like pointed out by Kirill Tkhai we could reusedelayed_put_task_structmethod that is already used by exit, except it does more than freeing the task structure and it relies on refcounters to be initialized so I thought thefree_task->call_rcu( free_task)conversion was simpler and more obviously safe. Sharing the other method only looked a complication that requires syncing up the refcounts.
Quoted 4 lines
I think the only conceptual simplification possible would be again to add a new hooking point and more buildup of information until fork cannot fail, but in implementation terms I doubt the fix will become smaller or simpler that way.
Well, in general I prefer the code to be memcg neutral as much as possible. We might have this subtle dependency with memcg now but this is not specific to memcg in general. Therefore, if there is a way to make a userfault specific fix then I would prefer it. If that is not feasible then fair enough.
JFYI, getting rid of mm->owner is a long term plan. This is just too ugly to live. Easier said than done, unfortunately.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.All the
CONFIG_MEMCGis just ugly as hell. Can we reduce that please? E.g. use if (IS_ENABLED(CONFIG_MEMCG)) where appropriate?There's just one place where I could use that instead of #ifdef.
OK, I can see it now. Is there any strong reason to make the delayed freeing conditional that would spare at least part of the ugliness.
Quoted 11 lines
Quoted 9 lines
kernel/fork.c static void mm_init_aio(struct mm_struct *mm) · line 961
Quoted 7 lines
kernel/fork.c +7
-0+static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif How can we ever hit this warning and what does that mean?
Which warning?
A brain fart, I would have sworn that I've seen WARN_ON_ONCE. Sorry about the confusion.
Signature · 2 lines
Michal Hocko SUSE Labs
This patch is presently stuck. AFAICT we just need a changelog update to reflect Michal's observations?
From: Andrea Arcangeli <[email protected]>
Subject: userfaultfd: use RCU to free the task struct when fork fails
MEMCG depends on the task structure not to be freed under rcu_read_lock() in get_mem_cgroup_from_mm() after it dereferences mm->owner.
An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after fork() is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to set mm->owner to NULL and to free the task struct that was pointed by mm->owner with RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.
This race condition couldn't trigger if CONFIG_MEMCG was set =n at build time.
---
kernel/fork.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)
kernel/fork.c renamed from kernel/fork.c~userfaultfd-use-rcu-to-free-the-task-struct-when-fork-fails +32 -2
@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG
@@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct t free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:
@@ -1662,6 +1672,24 @@ static inline void rcu_copy_process(stru#endif /* #ifdef CONFIG_TASKS_RCU */ } +#ifdef CONFIG_MEMCG +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +} +#endif /* CONFIG_MEMCG */ + +static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +#ifdef CONFIG_MEMCG +call_rcu (&tsk->rcu, __delayed_free_task); +#else /* CONFIG_MEMCG */ +free_task (tsk); +#endif /* CONFIG_MEMCG */ +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet.
@@ -2123,8 +2151,10 @@ bad_fork_cleanup_io: bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);
@@ -2155,7 +2185,7 @@ bad_fork_cleanup_count: bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p); fork_out:spin_lock_irq (¤t->sighand->siglock);hlist_del_init (&delayed.node);
_
PATCH v2 1/1 userfaultfd: use RCU to free the task struct when fork fails
In reply to Michal HockoThe task structure is freed while get_mem_cgroup_from_mm() holds rcu_read_lock() and dereferences mm->owner.
get_mem_cgroup_from_mm() failing fork()
---- ---
task = mm->owner
mm->owner = NULL;
free(task)if (task) *task; /* use after free */
The fix consists in freeing the task with RCU also in the fork failure case, exactly like it always happens for the regular exit(2) path. That is enough to make the rcu_read_lock hold in get_mem_cgroup_from_mm() (left side above) effective to avoid a use after free when dereferencing the task structure.
An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after fork() is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to set mm->owner to NULL and to free the task struct that was pointed by mm->owner with RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.
This race condition couldn't trigger if CONFIG_MEMCG was set =n at build time.
v2: improved commit header and reduced #ifdef material suggested by Michal Hocko.
--- kernel/fork.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
kernel/fork.c +29 -2
@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct *mm)#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG
@@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:
@@ -1662,6 +1672,21 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +} + +static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +if (IS_ENABLED (CONFIG_MEMCG )) +call_rcu (&tsk->rcu, __delayed_free_task); +else +free_task (tsk); +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet.
@@ -2123,8 +2148,10 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);
@@ -2155,7 +2182,7 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p); fork_out:spin_lock_irq (¤t->sighand->siglock);hlist_del_init (&delayed.node);
Quoted 72 lines · Andrea Arcangeli
The task structure is freed while
get_mem_cgroup_from_mm()holdsrcu_read_lock()and dereferencesmm->owner.
get_mem_cgroup_from_mm()failingfork()
---- ---
task =mm->ownermm->owner = NULL; free(task)if (task) *task; /* use after free */
The fix consists in freeing the task with RCU also in the fork failure case, exactly like it always happens for the regular exit(2) path. That is enough to make the
rcu_read_lockhold inget_mem_cgroup_from_mm()(left side above) effective to avoid a use after free when dereferencing the task structure.An alternate possible fix would be to defer the delivery of the userfaultfd contexts to the monitor until after
fork()is guaranteed to succeed. Such a change would require more changes because it would create a strict ordering dependency where the uffd methods would need to be called beyond the last potentially failing branch in order to be safe. This solution as opposed only adds the dependency to common code to setmm->ownerto NULL and to free the task struct that was pointed bymm->ownerwith RCU, if fork ends up failing. The userfaultfd methods can still be called anywhere during the fork runtime and the monitor will keep discarding orphaned "mm" coming from failed forks in userland.This race condition couldn't trigger if
CONFIG_MEMCGwas set =n at build time.v2: improved commit header and reduced #ifdef material suggested by Michal Hocko.
Fixes 893e26e61d04 ("userfaultfd: non-cooperative: Add fork() event")Tested-by zhong jiang <[email protected]>Reported-by [email protected]Signed-off-by Andrea Arcangeli <[email protected]>--- kernel/fork.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)kernel/fork.c +10
-0@@ -952,6 +952,15 @@ static void mm_init_aio(struct mm_struct *mm)#endif } +static __always_inline void mm_clear_owner(struct mm_struct *mm, +struct task_struct *p) +{ +#ifdef CONFIG_MEMCG +if (mm->owner == p) +WRITE_ONCE (mm->owner,NULL ); +#endif +} +static void mm_init_owner (struct mm_struct *mm,struct task_struct *p) {#ifdef CONFIG_MEMCG @@ -1331,6 +1340,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) free_pt:/* don't put binfmt in mmput, we haven't got module yet */ mm->binfmt =NULL ; +mm_init_owner (mm,NULL );mmput (mm); fail_nomem:
kernel/fork.c static inline void rcu_copy_process(struct task_struct *p) · line 1680
Quoted 9 lines
kernel/fork.c +6
-0@@ -1662,6 +1672,21 @@ static inline void rcu_copy_process(struct task_struct *p)#endif /* #ifdef CONFIG_TASKS_RCU */ } +static void __delayed_free_task (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); + +free_task (tsk); +}
if we disable the CONFIG_MEMCG, __delay_free_task will not to be used.
Thanks,
zhong jiang
Quoted 35 lines
kernel/fork.c +12
-2+static __always_inline void delayed_free_task(struct task_struct *tsk) +{ +if (IS_ENABLED (CONFIG_MEMCG )) +call_rcu (&tsk->rcu, __delayed_free_task); +else +free_task (tsk); +} +/* * This creates a new process as a copy of the old one, * but does not actually start it yet. @@ -2123,8 +2148,10 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_cleanup_namespaces:exit_task_namespaces (p); bad_fork_cleanup_mm: -if (p->mm) +if (p->mm) { +mm_clear_owner (p->mm, p);mmput (p->mm); + } bad_fork_cleanup_signal:if (!(clone_flags &CLONE_THREAD ))free_signal_struct (p->signal);@@ -2155,7 +2182,7 @@ static __latent_entropy struct task_struct *copy_process( bad_fork_free: p->state =TASK_DEAD ;put_task_stack (p); -free_task (p); +delayed_free_task (p); fork_out:spin_lock_irq (¤t->sighand->siglock);hlist_del_init (&delayed.node);.
Hello,
Quoted 1 line · zhong jiang
if we disable the
CONFIG_MEMCG,__delay_free_taskwill not to be used.
Yes, the compiler optimizes that away at build time.
Thanks,
Andrea
PATCH 2/2 mm: change mm_update_next_owner() to update mm->owner with WRITE_ONCE
In reply to Andrea ArcangeliThe RCU reader uses rcu_dereference() inside rcu_read_lock critical sections, so the writer shall use WRITE_ONCE. Just a cleanup, we still rely on gcc to emit atomic writes in other places.
--- kernel/exit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/exit.c +3 -3
@@ -422,7 +422,7 @@ void mm_update_next_owner(struct mm_struct *mm)* freed task structure. */ if (atomic_read (&mm->mm_users) <=1 ) { - mm->owner =NULL ; +WRITE_ONCE (mm->owner,NULL );return ; }
@@ -462,7 +462,7 @@ void mm_update_next_owner(struct mm_struct *mm)* most likely racing with swapoff (try_to_unuse()) or /proc or * ptrace or page migration (get_task_mm()). Mark owner as NULL. */ - mm->owner =NULL ; +WRITE_ONCE (mm->owner,NULL );return ; assign_new_owner:
@@ -483,7 +483,7 @@ void mm_update_next_owner(struct mm_struct *mm)put_task_struct (c); goto retry; } - mm->owner = c; +WRITE_ONCE (mm->owner, c);task_unlock (c);put_task_struct (c); }