linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/mempolicy: Fix redundant check and refine lock protection scope in init_nodemask_of_mempolicy
@ 2024-11-21 12:24 Zhen Ni
  2024-11-21 13:40 ` David Hildenbrand
  0 siblings, 1 reply; 4+ messages in thread
From: Zhen Ni @ 2024-11-21 12:24 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, Zhen Ni

1.Removing redundant checks for current->mempolicy, with a more concise
check order.

2.Using READ_ONCE(current->mempolicy) for safe, single access to
current->mempolicy to prevent potential race conditions.

3.Optimizing the scope of task_lock(current). The lock now only protects
the critical section where mempolicy is accessed, reducing the duration
the lock is held. This enhances performance by limiting the scope of the
lock to only what is necessary.

Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
---
 mm/mempolicy.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index b646fab3e45e..8bff8830b7e6 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2132,11 +2132,14 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
 {
 	struct mempolicy *mempolicy;
 
-	if (!(mask && current->mempolicy))
+	if (!mask)
+		return false;
+
+	mempolicy = READ_ONCE(current->mempolicy);
+	if (!mempolicy)
 		return false;
 
 	task_lock(current);
-	mempolicy = current->mempolicy;
 	switch (mempolicy->mode) {
 	case MPOL_PREFERRED:
 	case MPOL_PREFERRED_MANY:
-- 
2.20.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-11-22  3:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-21 12:24 [PATCH] mm/mempolicy: Fix redundant check and refine lock protection scope in init_nodemask_of_mempolicy Zhen Ni
2024-11-21 13:40 ` David Hildenbrand
2024-11-21 14:41   ` Gregory Price
2024-11-22  3:54     ` zhen.ni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox