From: Zhongkun He <hezhongkun.hzk@bytedance.com>
To: mhocko@suse.com, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org, wuyun.abel@bytedance.com,
Zhongkun He <hezhongkun.hzk@bytedance.com>
Subject: [PATCH 3/3] mm: add __rcu symbol for task->mempolicy
Date: Mon, 5 Dec 2022 00:14:32 +0800 [thread overview]
Message-ID: <20221204161432.2149375-4-hezhongkun.hzk@bytedance.com> (raw)
In-Reply-To: <20221204161432.2149375-1-hezhongkun.hzk@bytedance.com>
The task->mempolicy is protected by task_lock in slow path,
but there is no locking and reference in hot path for performance.
It will be difficult if other processes want to adjust it.
It is for these reasons to add __rcu symbol for task mempolicy.
There is no need to add RCU protection to vma mempolicy, which
is protected by mmap_lock.
Suggested-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Zhongkun He <hezhongkun.hzk@bytedance.com>
---
include/linux/sched.h | 2 +-
mm/mempolicy.c | 9 ++++-----
mm/slab.c | 5 +++--
3 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index ffb6eb55cd13..c8a297ca61ab 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1252,7 +1252,7 @@ struct task_struct {
#endif
#ifdef CONFIG_NUMA
/* Protected by alloc_lock: */
- struct mempolicy *mempolicy;
+ struct mempolicy __rcu *mempolicy;
short il_prev;
short pref_node_fork;
#endif
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 0feffb7ff01e..837083fff9c8 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -894,8 +894,7 @@ static long do_set_mempolicy(unsigned short mode, unsigned short flags,
goto out;
}
- old = current->mempolicy;
- current->mempolicy = new;
+ old = rcu_replace_pointer(current->mempolicy, new, true);
if (new && new->mode == MPOL_INTERLEAVE)
current->il_prev = MAX_NUMNODES-1;
task_unlock(current);
@@ -999,7 +998,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask,
if (err < 0)
goto out;
*policy = err;
- } else if (pol == current->mempolicy &&
+ } else if (pol == rcu_access_pointer(current->mempolicy) &&
pol->mode == MPOL_INTERLEAVE) {
*policy = next_node_in(current->il_prev, pol->nodes);
} else {
@@ -2065,7 +2064,7 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
{
struct mempolicy *mempolicy;
- if (!(mask && current->mempolicy))
+ if (!(mask && rcu_access_pointer(current->mempolicy)))
return false;
task_lock(current);
@@ -2426,7 +2425,7 @@ struct mempolicy *__mpol_dup(struct mempolicy *old)
return ERR_PTR(-ENOMEM);
/* task's mempolicy is protected by alloc_lock */
- if (old == current->mempolicy) {
+ if (old == rcu_access_pointer(current->mempolicy)) {
task_lock(current);
*new = *old;
task_unlock(current);
diff --git a/mm/slab.c b/mm/slab.c
index 59c8e28f7b6a..f205869d6c36 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -3052,7 +3052,7 @@ static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
nid_alloc = nid_here = numa_mem_id();
if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
nid_alloc = cpuset_slab_spread_node();
- else if (current->mempolicy)
+ else if (rcu_access_pointer(current->mempolicy))
nid_alloc = mempolicy_slab_node();
if (nid_alloc != nid_here)
return ____cache_alloc_node(cachep, flags, nid_alloc);
@@ -3188,7 +3188,8 @@ __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags, int nodeid)
int slab_node = numa_mem_id();
if (nodeid == NUMA_NO_NODE) {
- if (current->mempolicy || cpuset_do_slab_mem_spread()) {
+ if (rcu_access_pointer(current->mempolicy) ||
+ cpuset_do_slab_mem_spread()) {
objp = alternate_node_alloc(cachep, flags);
if (objp)
goto out;
--
2.25.1
next prev parent reply other threads:[~2022-12-04 16:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-04 16:14 [PATCH 0/3] mm: replace atomic_t with percpu_ref in mempolicy Zhongkun He
2022-12-04 16:14 ` [PATCH 1/3] mm: replace mpol_put() with mpol_kill() initiated the destruction of mpol Zhongkun He
2022-12-04 16:14 ` [PATCH 2/3] mm: fix the reference of mempolicy in some functions Zhongkun He
2022-12-04 16:14 ` Zhongkun He [this message]
2022-12-04 18:25 ` [PATCH 0/3] mm: replace atomic_t with percpu_ref in mempolicy kernel test robot
2023-01-13 16:20 ` Michal Hocko
2023-01-13 16:22 ` Michal Hocko
2023-01-14 16:10 ` [External] " Zhongkun He
2023-01-13 17:04 ` Michal Hocko
2023-01-14 16:52 ` [External] " Zhongkun He
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=20221204161432.2149375-4-hezhongkun.hzk@bytedance.com \
--to=hezhongkun.hzk@bytedance.com \
--cc=akpm@linux-foundation.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--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