Back to home page

LXR

 
 

    


0001 /* Function to determine if a thread group is single threaded or not
0002  *
0003  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
0004  * Written by David Howells (dhowells@redhat.com)
0005  * - Derived from security/selinux/hooks.c
0006  *
0007  * This program is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU General Public Licence
0009  * as published by the Free Software Foundation; either version
0010  * 2 of the Licence, or (at your option) any later version.
0011  */
0012 
0013 #include <linux/sched.h>
0014 
0015 /*
0016  * Returns true if the task does not share ->mm with another thread/process.
0017  */
0018 bool current_is_single_threaded(void)
0019 {
0020     struct task_struct *task = current;
0021     struct mm_struct *mm = task->mm;
0022     struct task_struct *p, *t;
0023     bool ret;
0024 
0025     if (atomic_read(&task->signal->live) != 1)
0026         return false;
0027 
0028     if (atomic_read(&mm->mm_users) == 1)
0029         return true;
0030 
0031     ret = false;
0032     rcu_read_lock();
0033     for_each_process(p) {
0034         if (unlikely(p->flags & PF_KTHREAD))
0035             continue;
0036         if (unlikely(p == task->group_leader))
0037             continue;
0038 
0039         for_each_thread(p, t) {
0040             if (unlikely(t->mm == mm))
0041                 goto found;
0042             if (likely(t->mm))
0043                 break;
0044             /*
0045              * t->mm == NULL. Make sure next_thread/next_task
0046              * will see other CLONE_VM tasks which might be
0047              * forked before exiting.
0048              */
0049             smp_rmb();
0050         }
0051     }
0052     ret = true;
0053 found:
0054     rcu_read_unlock();
0055 
0056     return ret;
0057 }