Back to home page

OSCL-LXR

 
 

    


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