Skip to contentSkip to conversation

get_task_mm: check PF_KTHREAD lockless

PATCH Applied Oleg Nesterov 2 participants 2 messages Jun 26, 2024 – Jun 27, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 get_task_mm: check PF_KTHREAD lockless

Nowadays PF_KTHREAD is sticky and it was never protected by ->alloc_lock. Move the PF_KTHREAD check outside of task_lock() section to make this code more understandable.

---
 kernel/fork.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/fork.c +5 -6
index 4c361d2bdc12..d3dc623a1247 100644
@@ -1536,14 +1536,13 @@ struct mm_struct *get_task_mm(struct task_struct *task)
 {
 	struct mm_struct *mm;
 
+	if (task->flags & PF_KTHREAD)
+		return NULL;
+
 	task_lock(task);
 	mm = task->mm;
-	if (mm) {
-		if (task->flags & PF_KTHREAD)
-			mm = NULL;
-		else
-			mmget(mm);
-	}
+	if (mm)
+		mmget(mm);
 	task_unlock(task);
 	return mm;
 }
Signature · 1 line
2.25.1.362.g51ebf55
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Oleg Nesterov

Nowadays PF_KTHREAD is sticky and it was never protected by ->alloc_lock. Move the PF_KTHREAD check outside of task_lock() section to make this code more understandable.

Thanks!

Quoted 31 lines
---
 kernel/fork.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/fork.c +5 -6
index 4c361d2bdc12..d3dc623a1247 100644
@@ -1536,14 +1536,13 @@ struct mm_struct *get_task_mm(struct task_struct *task)
 {
 	struct mm_struct *mm;
 
+	if (task->flags & PF_KTHREAD)
+		return NULL;
+
 	task_lock(task);
 	mm = task->mm;
-	if (mm) {
-		if (task->flags & PF_KTHREAD)
-			mm = NULL;
-		else
-			mmget(mm);
-	}
+	if (mm)
+		mmget(mm);
 	task_unlock(task);
 	return mm;
 }
Signature · 1 line
2.25.1.362.g51ebf55
Signature · 2 lines
Michal Hocko
SUSE Labs