1*b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 26cc88bc4SDavid Howells /* Function to determine if a thread group is single threaded or not 36cc88bc4SDavid Howells * 46cc88bc4SDavid Howells * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. 56cc88bc4SDavid Howells * Written by David Howells (dhowells@redhat.com) 66cc88bc4SDavid Howells * - Derived from security/selinux/hooks.c 76cc88bc4SDavid Howells */ 83f07c014SIngo Molnar #include <linux/sched/signal.h> 99164bb4aSIngo Molnar #include <linux/sched/task.h> 10589ee628SIngo Molnar #include <linux/sched/mm.h> 116cc88bc4SDavid Howells 12d2e3ee9bSOleg Nesterov /* 13d2e3ee9bSOleg Nesterov * Returns true if the task does not share ->mm with another thread/process. 146cc88bc4SDavid Howells */ current_is_single_threaded(void)155bb459bbSOleg Nesterovbool current_is_single_threaded(void) 166cc88bc4SDavid Howells { 175bb459bbSOleg Nesterov struct task_struct *task = current; 18d2e3ee9bSOleg Nesterov struct mm_struct *mm = task->mm; 19d2e3ee9bSOleg Nesterov struct task_struct *p, *t; 20d2e3ee9bSOleg Nesterov bool ret; 216cc88bc4SDavid Howells 22d2e3ee9bSOleg Nesterov if (atomic_read(&task->signal->live) != 1) 23d2e3ee9bSOleg Nesterov return false; 246cc88bc4SDavid Howells 25d2e3ee9bSOleg Nesterov if (atomic_read(&mm->mm_users) == 1) 266cc88bc4SDavid Howells return true; 276cc88bc4SDavid Howells 28d2e3ee9bSOleg Nesterov ret = false; 29d2e3ee9bSOleg Nesterov rcu_read_lock(); 30d2e3ee9bSOleg Nesterov for_each_process(p) { 31d2e3ee9bSOleg Nesterov if (unlikely(p->flags & PF_KTHREAD)) 32d2e3ee9bSOleg Nesterov continue; 33d2e3ee9bSOleg Nesterov if (unlikely(p == task->group_leader)) 34d2e3ee9bSOleg Nesterov continue; 35d2e3ee9bSOleg Nesterov 3690224350SOleg Nesterov for_each_thread(p, t) { 37d2e3ee9bSOleg Nesterov if (unlikely(t->mm == mm)) 38d2e3ee9bSOleg Nesterov goto found; 39d2e3ee9bSOleg Nesterov if (likely(t->mm)) 40d2e3ee9bSOleg Nesterov break; 41967cc537SOleg Nesterov /* 42967cc537SOleg Nesterov * t->mm == NULL. Make sure next_thread/next_task 43967cc537SOleg Nesterov * will see other CLONE_VM tasks which might be 44967cc537SOleg Nesterov * forked before exiting. 45967cc537SOleg Nesterov */ 46967cc537SOleg Nesterov smp_rmb(); 4790224350SOleg Nesterov } 48d2e3ee9bSOleg Nesterov } 49d2e3ee9bSOleg Nesterov ret = true; 50d2e3ee9bSOleg Nesterov found: 51d2e3ee9bSOleg Nesterov rcu_read_unlock(); 52d2e3ee9bSOleg Nesterov 53d2e3ee9bSOleg Nesterov return ret; 546cc88bc4SDavid Howells } 55