16cc88bc4SDavid Howells /* Function to determine if a thread group is single threaded or not 26cc88bc4SDavid Howells * 36cc88bc4SDavid Howells * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. 46cc88bc4SDavid Howells * Written by David Howells (dhowells@redhat.com) 56cc88bc4SDavid Howells * - Derived from security/selinux/hooks.c 66cc88bc4SDavid Howells * 76cc88bc4SDavid Howells * This program is free software; you can redistribute it and/or 86cc88bc4SDavid Howells * modify it under the terms of the GNU General Public Licence 96cc88bc4SDavid Howells * as published by the Free Software Foundation; either version 106cc88bc4SDavid Howells * 2 of the Licence, or (at your option) any later version. 116cc88bc4SDavid Howells */ 126cc88bc4SDavid Howells 136cc88bc4SDavid Howells #include <linux/sched.h> 146cc88bc4SDavid Howells 15*d2e3ee9bSOleg Nesterov /* 16*d2e3ee9bSOleg Nesterov * Returns true if the task does not share ->mm with another thread/process. 176cc88bc4SDavid Howells */ 18*d2e3ee9bSOleg Nesterov bool is_single_threaded(struct task_struct *task) 196cc88bc4SDavid Howells { 20*d2e3ee9bSOleg Nesterov struct mm_struct *mm = task->mm; 21*d2e3ee9bSOleg Nesterov struct task_struct *p, *t; 22*d2e3ee9bSOleg Nesterov bool ret; 236cc88bc4SDavid Howells 24*d2e3ee9bSOleg Nesterov might_sleep(); 256cc88bc4SDavid Howells 26*d2e3ee9bSOleg Nesterov if (atomic_read(&task->signal->live) != 1) 27*d2e3ee9bSOleg Nesterov return false; 286cc88bc4SDavid Howells 29*d2e3ee9bSOleg Nesterov if (atomic_read(&mm->mm_users) == 1) 306cc88bc4SDavid Howells return true; 316cc88bc4SDavid Howells 32*d2e3ee9bSOleg Nesterov ret = false; 33*d2e3ee9bSOleg Nesterov down_write(&mm->mmap_sem); 34*d2e3ee9bSOleg Nesterov rcu_read_lock(); 35*d2e3ee9bSOleg Nesterov for_each_process(p) { 36*d2e3ee9bSOleg Nesterov if (unlikely(p->flags & PF_KTHREAD)) 37*d2e3ee9bSOleg Nesterov continue; 38*d2e3ee9bSOleg Nesterov if (unlikely(p == task->group_leader)) 39*d2e3ee9bSOleg Nesterov continue; 40*d2e3ee9bSOleg Nesterov 41*d2e3ee9bSOleg Nesterov t = p; 42*d2e3ee9bSOleg Nesterov do { 43*d2e3ee9bSOleg Nesterov if (unlikely(t->mm == mm)) 44*d2e3ee9bSOleg Nesterov goto found; 45*d2e3ee9bSOleg Nesterov if (likely(t->mm)) 46*d2e3ee9bSOleg Nesterov break; 47*d2e3ee9bSOleg Nesterov } while_each_thread(p, t); 48*d2e3ee9bSOleg Nesterov } 49*d2e3ee9bSOleg Nesterov ret = true; 50*d2e3ee9bSOleg Nesterov found: 51*d2e3ee9bSOleg Nesterov rcu_read_unlock(); 52*d2e3ee9bSOleg Nesterov up_write(&mm->mmap_sem); 53*d2e3ee9bSOleg Nesterov 54*d2e3ee9bSOleg Nesterov return ret; 556cc88bc4SDavid Howells } 56