get_task_exe_file: check PF_KTHREAD locklessly
authorMateusz Guzik <mjguzik@gmail.com>
Tue, 19 Nov 2024 14:35:26 +0000 (15:35 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 13 Jan 2025 04:21:06 +0000 (20:21 -0800)
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.

Link: https://lkml.kernel.org/r/20241119143526.704986-1-mjguzik@gmail.com
Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/fork.c

index 9b301180fd41628f55fc318d8899cab2698ba6f5..19d7fe31869bf3055ef193ffd832409bf101abe3 100644 (file)
@@ -1514,12 +1514,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;
 }