From: Oleg Nesterov Date: Wed, 26 Jun 2024 19:10:17 +0000 (+0200) Subject: get_task_mm: check PF_KTHREAD lockless X-Git-Tag: v6.11-rc1~85^2~112 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8ac5dc66599c;p=thirdparty%2Flinux.git 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/20240626191017.GA20031@redhat.com Signed-off-by: Oleg Nesterov Acked-by: Michal Hocko Cc: Eric W. Biederman Signed-off-by: Andrew Morton --- diff --git a/kernel/fork.c b/kernel/fork.c index 99076dbe27d83..279efadabbf22 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -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; }