task.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SCHED_TASK_H
  3. #define _LINUX_SCHED_TASK_H
  4. /*
  5. * Interface between the scheduler and various task lifetime (fork()/exit())
  6. * functionality:
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/uaccess.h>
  10. struct task_struct;
  11. struct rusage;
  12. union thread_union;
  13. struct css_set;
  14. /* All the bits taken by the old clone syscall. */
  15. #define CLONE_LEGACY_FLAGS 0xffffffffULL
  16. struct kernel_clone_args {
  17. u64 flags;
  18. int __user *pidfd;
  19. int __user *child_tid;
  20. int __user *parent_tid;
  21. int exit_signal;
  22. unsigned long stack;
  23. unsigned long stack_size;
  24. unsigned long tls;
  25. pid_t *set_tid;
  26. /* Number of elements in *set_tid */
  27. size_t set_tid_size;
  28. int cgroup;
  29. int io_thread;
  30. int kthread;
  31. int idle;
  32. int (*fn)(void *);
  33. void *fn_arg;
  34. struct cgroup *cgrp;
  35. struct css_set *cset;
  36. };
  37. /*
  38. * This serializes "schedule()" and also protects
  39. * the run-queue from deletions/modifications (but
  40. * _adding_ to the beginning of the run-queue has
  41. * a separate lock).
  42. */
  43. extern rwlock_t tasklist_lock;
  44. extern spinlock_t mmlist_lock;
  45. extern union thread_union init_thread_union;
  46. extern struct task_struct init_task;
  47. extern int lockdep_tasklist_lock_is_held(void);
  48. extern asmlinkage void schedule_tail(struct task_struct *prev);
  49. extern void init_idle(struct task_struct *idle, int cpu);
  50. extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
  51. extern void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs);
  52. extern void sched_post_fork(struct task_struct *p);
  53. extern void sched_dead(struct task_struct *p);
  54. void __noreturn do_task_dead(void);
  55. void __noreturn make_task_dead(int signr);
  56. extern void mm_cache_init(void);
  57. extern void proc_caches_init(void);
  58. extern void fork_init(void);
  59. extern void release_task(struct task_struct * p);
  60. extern int copy_thread(struct task_struct *, const struct kernel_clone_args *);
  61. extern void flush_thread(void);
  62. #ifdef CONFIG_HAVE_EXIT_THREAD
  63. extern void exit_thread(struct task_struct *tsk);
  64. #else
  65. static inline void exit_thread(struct task_struct *tsk)
  66. {
  67. }
  68. #endif
  69. extern __noreturn void do_group_exit(int);
  70. extern void exit_files(struct task_struct *);
  71. extern void exit_itimers(struct task_struct *);
  72. extern pid_t kernel_clone(struct kernel_clone_args *kargs);
  73. struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node);
  74. struct task_struct *fork_idle(int);
  75. extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  76. extern pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags);
  77. extern long kernel_wait4(pid_t, int __user *, int, struct rusage *);
  78. int kernel_wait(pid_t pid, int *stat);
  79. extern void free_task(struct task_struct *tsk);
  80. /* sched_exec is called by processes performing an exec */
  81. #ifdef CONFIG_SMP
  82. extern void sched_exec(void);
  83. #else
  84. #define sched_exec() {}
  85. #endif
  86. static inline struct task_struct *get_task_struct(struct task_struct *t)
  87. {
  88. refcount_inc(&t->usage);
  89. return t;
  90. }
  91. extern void __put_task_struct(struct task_struct *t);
  92. extern void __put_task_struct_rcu_cb(struct rcu_head *rhp);
  93. static inline void put_task_struct(struct task_struct *t)
  94. {
  95. if (!refcount_dec_and_test(&t->usage))
  96. return;
  97. /*
  98. * under PREEMPT_RT, we can't call put_task_struct
  99. * in atomic context because it will indirectly
  100. * acquire sleeping locks.
  101. *
  102. * call_rcu() will schedule delayed_put_task_struct_rcu()
  103. * to be called in process context.
  104. *
  105. * __put_task_struct() is called when
  106. * refcount_dec_and_test(&t->usage) succeeds.
  107. *
  108. * This means that it can't "conflict" with
  109. * put_task_struct_rcu_user() which abuses ->rcu the same
  110. * way; rcu_users has a reference so task->usage can't be
  111. * zero after rcu_users 1 -> 0 transition.
  112. *
  113. * delayed_free_task() also uses ->rcu, but it is only called
  114. * when it fails to fork a process. Therefore, there is no
  115. * way it can conflict with put_task_struct().
  116. */
  117. if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible())
  118. call_rcu(&t->rcu, __put_task_struct_rcu_cb);
  119. else
  120. __put_task_struct(t);
  121. }
  122. static inline void put_task_struct_many(struct task_struct *t, int nr)
  123. {
  124. if (refcount_sub_and_test(nr, &t->usage))
  125. __put_task_struct(t);
  126. }
  127. void put_task_struct_rcu_user(struct task_struct *task);
  128. /* Free all architecture-specific resources held by a thread. */
  129. void release_thread(struct task_struct *dead_task);
  130. #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
  131. extern int arch_task_struct_size __read_mostly;
  132. #else
  133. # define arch_task_struct_size (sizeof(struct task_struct))
  134. #endif
  135. #ifndef CONFIG_HAVE_ARCH_THREAD_STRUCT_WHITELIST
  136. /*
  137. * If an architecture has not declared a thread_struct whitelist we
  138. * must assume something there may need to be copied to userspace.
  139. */
  140. static inline void arch_thread_struct_whitelist(unsigned long *offset,
  141. unsigned long *size)
  142. {
  143. *offset = 0;
  144. /* Handle dynamically sized thread_struct. */
  145. *size = arch_task_struct_size - offsetof(struct task_struct, thread);
  146. }
  147. #endif
  148. #ifdef CONFIG_VMAP_STACK
  149. static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
  150. {
  151. return t->stack_vm_area;
  152. }
  153. #else
  154. static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
  155. {
  156. return NULL;
  157. }
  158. #endif
  159. /*
  160. * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
  161. * subscriptions and synchronises with wait4(). Also used in procfs. Also
  162. * pins the final release of task.io_context. Also protects ->cpuset and
  163. * ->cgroup.subsys[]. And ->vfork_done. And ->sysvshm.shm_clist.
  164. *
  165. * Nests both inside and outside of read_lock(&tasklist_lock).
  166. * It must not be nested with write_lock_irq(&tasklist_lock),
  167. * neither inside nor outside.
  168. */
  169. static inline void task_lock(struct task_struct *p)
  170. {
  171. spin_lock(&p->alloc_lock);
  172. }
  173. static inline void task_unlock(struct task_struct *p)
  174. {
  175. spin_unlock(&p->alloc_lock);
  176. }
  177. #endif /* _LINUX_SCHED_TASK_H */