Re: [PATCH 1/1] mm, oom_adj: don't loop through tasks in __set_oom_adj when not necessary

From: Christian Brauner
Date: Thu Aug 20 2020 - 09:46:38 EST


On Thu, Aug 20, 2020 at 02:41:09PM +0200, Michal Hocko wrote:
> On Thu 20-08-20 13:42:56, Michal Hocko wrote:
> > On Thu 20-08-20 13:30:23, Christian Brauner wrote:
> [...]
> > > trying to rely on set_bit() and test_bit() in copy_mm() being atomic and
> > > then calling it where Oleg said after the point of no return.
> >
> > No objections.
>
> Would something like the following work for you?
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 9177a76bf840..25b83f0912a6 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -1403,15 +1403,6 @@ static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
> if (clone_flags & CLONE_VM) {
> mmget(oldmm);
> mm = oldmm;
> - if (!(clone_flags & CLONE_SIGHAND)) {
> - /* We need to synchronize with __set_oom_adj */
> - mutex_lock(&oom_adj_lock);
> - set_bit(MMF_PROC_SHARED, &mm->flags);
> - /* Update the values in case they were changed after copy_signal */
> - tsk->signal->oom_score_adj = current->signal->oom_score_adj;
> - tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min;
> - mutex_unlock(&oom_adj_lock);
> - }
> goto good_mm;
> }
>
> @@ -1818,6 +1809,19 @@ static __always_inline void delayed_free_task(struct task_struct *tsk)
> free_task(tsk);
> }
>
> +static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk)
> +{
> + if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) == CLONE_VM) {
> + /* We need to synchronize with __set_oom_adj */
> + mutex_lock(&oom_adj_lock);
> + set_bit(MMF_PROC_SHARED, &mm->flags);
> + /* Update the values in case they were changed after copy_signal */
> + tsk->signal->oom_score_adj = current->signal->oom_score_adj;
> + tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min;
> + mutex_unlock(&oom_adj_lock);
> + }
> +}
> +
> /*
> * This creates a new process as a copy of the old one,
> * but does not actually start it yet.
> @@ -2290,6 +2294,8 @@ static __latent_entropy struct task_struct *copy_process(
> trace_task_newtask(p, clone_flags);
> uprobe_copy_process(p, clone_flags);
>
> + copy_oom_score_adj(clone_flags, p);
> +
> return p;
>
> bad_fork_cancel_cgroup:

This should work, yes.
And keeps the patch reasonably small.

Christian