b4d0d230ccfb5 (Thomas Gleixner 2019-05-20 19:08:01 +0200 1) // SPDX-License-Identifier: GPL-2.0-or-later
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 2) /* Function to determine if a thread group is single threaded or not
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 3) *
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 4) * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 5) * Written by David Howells (dhowells@redhat.com)
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 6) * - Derived from security/selinux/hooks.c
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 7) */
3f07c0144132e (Ingo Molnar 2017-02-08 18:51:30 +0100 8) #include <linux/sched/signal.h>
9164bb4a18dfa (Ingo Molnar 2017-02-04 01:20:53 +0100 9) #include <linux/sched/task.h>
589ee62844e04 (Ingo Molnar 2017-02-04 00:16:44 +0100 10) #include <linux/sched/mm.h>
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 11)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 12) /*
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 13) * Returns true if the task does not share ->mm with another thread/process.
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 14) */
5bb459bb45d1a (Oleg Nesterov 2009-07-10 03:48:23 +0200 15) bool current_is_single_threaded(void)
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 16) {
5bb459bb45d1a (Oleg Nesterov 2009-07-10 03:48:23 +0200 17) struct task_struct *task = current;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 18) struct mm_struct *mm = task->mm;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 19) struct task_struct *p, *t;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 20) bool ret;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 21)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 22) if (atomic_read(&task->signal->live) != 1)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 23) return false;
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 24)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 25) if (atomic_read(&mm->mm_users) == 1)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 26) return true;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 27)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 28) ret = false;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 29) rcu_read_lock();
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 30) for_each_process(p) {
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 31) if (unlikely(p->flags & PF_KTHREAD))
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 32) continue;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 33) if (unlikely(p == task->group_leader))
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 34) continue;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 35)
90224350eaaf8 (Oleg Nesterov 2015-11-06 16:31:26 -0800 36) for_each_thread(p, t) {
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 37) if (unlikely(t->mm == mm))
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 38) goto found;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 39) if (likely(t->mm))
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 40) break;
967cc5371113f (Oleg Nesterov 2009-07-09 23:28:49 +0200 41) /*
967cc5371113f (Oleg Nesterov 2009-07-09 23:28:49 +0200 42) * t->mm == NULL. Make sure next_thread/next_task
967cc5371113f (Oleg Nesterov 2009-07-09 23:28:49 +0200 43) * will see other CLONE_VM tasks which might be
967cc5371113f (Oleg Nesterov 2009-07-09 23:28:49 +0200 44) * forked before exiting.
967cc5371113f (Oleg Nesterov 2009-07-09 23:28:49 +0200 45) */
967cc5371113f (Oleg Nesterov 2009-07-09 23:28:49 +0200 46) smp_rmb();
90224350eaaf8 (Oleg Nesterov 2015-11-06 16:31:26 -0800 47) }
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 48) }
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 49) ret = true;
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 50) found:
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 51) rcu_read_unlock();
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 52)
d2e3ee9b29f5d (Oleg Nesterov 2009-07-17 09:09:36 +1000 53) return ret;
6cc88bc45ce80 (David Howells 2008-11-14 10:39:21 +1100 54) }