Skip to contentSkip to conversation

get_task_exe_file: check PF_KTHREAD locklessly

PATCH Applied Mateusz Guzik 3 participants 3 messages Nov 19, 2024 – Nov 22, 2024

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

PATCH 1/1 get_task_exe_file: check PF_KTHREAD locklessly

Same thing as 8ac5dc66599c ("get_task_mm: check PF_KTHREAD lockless")

---
 kernel/fork.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/fork.c +5 -4
index 22f43721d031..759902828e13 100644
@@ -1500,12 +1500,13 @@ struct file *get_task_exe_file(struct task_struct *task)
 	struct file *exe_file = NULL;
 	struct mm_struct *mm;
 
+	if (task->flags & PF_KTHREAD)
+		return NULL;
+
 	task_lock(task);
 	mm = task->mm;
-	if (mm) {
-		if (!(task->flags & PF_KTHREAD))
-			exe_file = get_mm_exe_file(mm);
-	}
+	if (mm)
+		exe_file = get_mm_exe_file(mm);
 	task_unlock(task);
 	return exe_file;
 }
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Mateusz Guzik
kernel/fork.c +3 -0
@@ -1500,12 +1500,13 @@ struct file *get_task_exe_file(struct task_struct *task)
 	struct file *exe_file = NULL;
 	struct mm_struct *mm;
 
+	if (task->flags & PF_KTHREAD)
+		return NULL;
+

kernel/fork.c struct file *get_task_exe_file(struct task_struct *task) · line 1511

Quoted 10 lines
kernel/fork.c +2 -4
 	task_lock(task);
 	mm = task->mm;
-	if (mm) {
-		if (!(task->flags & PF_KTHREAD))
-			exe_file = get_mm_exe_file(mm);
-	}
+	if (mm)
+		exe_file = get_mm_exe_file(mm);
 	task_unlock(task);
 	return exe_file;
maintainer Acked-by Oleg Nesterov on v1 1/1
#2 lore raw
In reply to Mateusz Guzik
Quoted 1 line · On Tue, 19 Nov 2024 15:35:26 +0100 Mateusz Guzik <[email protected]> wrote:

Same thing as 8ac5dc66599c ("get_task_mm: check PF_KTHREAD lockless")

We prefer that patches have fuller changelogs, please.

I copied the 8ac5dc66599c changelog text into this patch's changelog.

From: Mateusz Guzik <[email protected]>
Subject: get_task_exe_file: check PF_KTHREAD locklessly
Date: Tue, 19 Nov 2024 15:35:26 +0100

Same thing as 8ac5dc66599c ("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 |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/fork.c renamed from kernel/fork.c~get_task_exe_file-check-pf_kthread-locklessly +5 -4
@@ -1496,12 +1496,13 @@ struct file *get_task_exe_file(struct ta
 	struct file *exe_file = NULL;
 	struct mm_struct *mm;
 
+	if (task->flags & PF_KTHREAD)
+		return NULL;
+
 	task_lock(task);
 	mm = task->mm;
-	if (mm) {
-		if (!(task->flags & PF_KTHREAD))
-			exe_file = get_mm_exe_file(mm);
-	}
+	if (mm)
+		exe_file = get_mm_exe_file(mm);
 	task_unlock(task);
 	return exe_file;
 }

_