mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: Abel Wu <wuyun.abel@bytedance.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Vlastimil Babka <vbabka@suse.cz>,
	Michal Hocko <mhocko@kernel.org>,
	Mel Gorman <mgorman@techsingularity.net>,
	Muchun Song <songmuchun@bytedance.com>,
	Linux MM <linux-mm@kvack.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] mm/mempolicy: fix lock contention on mems_allowed
Date: Thu, 18 Aug 2022 14:56:40 +0800	[thread overview]
Message-ID: <B9112128-B471-4369-8FAF-088635FE5535@linux.dev> (raw)
In-Reply-To: <20220811124157.74888-1-wuyun.abel@bytedance.com>



> On Aug 11, 2022, at 20:41, Abel Wu <wuyun.abel@bytedance.com> wrote:
> 
> The mems_allowed field can be modified by other tasks, so it isn't
> safe to access it with alloc_lock unlocked even in the current
> process context.
> 
> Say there are two tasks: A from cpusetA is performing set_mempolicy(2),
> and B is changing cpusetA's cpuset.mems:
> 
>  A (set_mempolicy)		B (echo xx > cpuset.mems)
>  -------------------------------------------------------
>  pol = mpol_new();
> 				update_tasks_nodemask(cpusetA) {
> 				  foreach t in cpusetA {
> 				    cpuset_change_task_nodemask(t) {
>  mpol_set_nodemask(pol) {
> 				      task_lock(t); // t could be A
>    new = f(A->mems_allowed);
> 				      update t->mems_allowed;
>    pol.create(pol, new);
> 				      task_unlock(t);
>  }
> 				    }
> 				  }
> 				}
>  task_lock(A);
>  A->mempolicy = pol;
>  task_unlock(A);
> 
> In this case A's pol->nodes is computed by old mems_allowed, and could
> be inconsistent with A's new mems_allowed.
> 
> While it is different when replacing vmas' policy: the pol->nodes is
> gone wild only when current_cpuset_is_being_rebound():
> 
>  A (mbind)			B (echo xx > cpuset.mems)
>  -------------------------------------------------------
>  pol = mpol_new();
>  mmap_write_lock(A->mm);
> 				cpuset_being_rebound = cpusetA;
> 				update_tasks_nodemask(cpusetA) {
> 				  foreach t in cpusetA {
> 				    cpuset_change_task_nodemask(t) {
>  mpol_set_nodemask(pol) {
> 				      task_lock(t); // t could be A
>    mask = f(A->mems_allowed);
> 				      update t->mems_allowed;
>    pol.create(pol, mask);
> 				      task_unlock(t);
>  }
> 				    }
>  foreach v in A->mm {
>    if (cpuset_being_rebound == cpusetA)
>      pol.rebind(pol, cpuset.mems);
>    v->vma_policy = pol;
>  }
>  mmap_write_unlock(A->mm);
> 				    mmap_write_lock(t->mm);
> 				    mpol_rebind_mm(t->mm);
> 				    mmap_write_unlock(t->mm);
> 				  }
> 				}
> 				cpuset_being_rebound = NULL;
> 
> In this case, the cpuset.mems, which has already done updating, is
> finally used for calculating pol->nodes, rather than A->mems_allowed.
> So it is OK to call mpol_set_nodemask() with alloc_lock unlocked when
> doing mbind(2).
> 
> Fixes: 78b132e9bae9 ("mm/mempolicy: remove or narrow the lock on current")
> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>

Reviewed-by: Muchun Song <songmuchun@bytedance.com>

Thanks.


      parent reply	other threads:[~2022-08-18  6:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-11 12:41 Abel Wu
     [not found] ` <YvUM7KaJaY+xnN2Y@dhcp22.suse.cz>
2022-08-11 14:11   ` Michal Hocko
2022-08-12  7:07     ` Michal Hocko
2022-08-20  2:06     ` Wei Yang
2022-08-18  6:56 ` Muchun Song [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B9112128-B471-4369-8FAF-088635FE5535@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mgorman@techsingularity.net \
    --cc=mhocko@kernel.org \
    --cc=songmuchun@bytedance.com \
    --cc=vbabka@suse.cz \
    --cc=wuyun.abel@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®