is_single_threaded.c 1.2 KB

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