Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

get_task_exe_file: check PF_KTHREAD locklessly

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>

authored by

Mateusz Guzik and committed by
Andrew Morton
51f8bd6d 93b6bd40

+5 -4
+5 -4
kernel/fork.c
··· 1514 1514 struct file *exe_file = NULL; 1515 1515 struct mm_struct *mm; 1516 1516 1517 + if (task->flags & PF_KTHREAD) 1518 + return NULL; 1519 + 1517 1520 task_lock(task); 1518 1521 mm = task->mm; 1519 - if (mm) { 1520 - if (!(task->flags & PF_KTHREAD)) 1521 - exe_file = get_mm_exe_file(mm); 1522 - } 1522 + if (mm) 1523 + exe_file = get_mm_exe_file(mm); 1523 1524 task_unlock(task); 1524 1525 return exe_file; 1525 1526 }