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