lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20221204161432.2149375-4-hezhongkun.hzk@bytedance.com>
Date:   Mon,  5 Dec 2022 00:14:32 +0800
From:   Zhongkun He <hezhongkun.hzk@...edance.com>
To:     mhocko@...e.com, akpm@...ux-foundation.org
Cc:     linux-mm@...ck.org, linux-kernel@...r.kernel.org,
        linux-doc@...r.kernel.org, wuyun.abel@...edance.com,
        Zhongkun He <hezhongkun.hzk@...edance.com>
Subject: [PATCH 3/3] mm: add __rcu symbol for task->mempolicy

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@...e.com>
Signed-off-by: Zhongkun He <hezhongkun.hzk@...edance.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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ