1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
229930025SIngo Molnar #ifndef _LINUX_SCHED_TASK_H
329930025SIngo Molnar #define _LINUX_SCHED_TASK_H
429930025SIngo Molnar
5901b14bdSIngo Molnar /*
6901b14bdSIngo Molnar * Interface between the scheduler and various task lifetime (fork()/exit())
7901b14bdSIngo Molnar * functionality:
8901b14bdSIngo Molnar */
9901b14bdSIngo Molnar
101e2f2d31SKent Overstreet #include <linux/rcupdate.h>
11f9d6966bSKent Overstreet #include <linux/refcount.h>
1229930025SIngo Molnar #include <linux/sched.h>
137f192e3cSChristian Brauner #include <linux/uaccess.h>
1429930025SIngo Molnar
15cdc75e9fSIngo Molnar struct task_struct;
1692ebce5aSAl Viro struct rusage;
17cdc75e9fSIngo Molnar union thread_union;
18ef2c41cfSChristian Brauner struct css_set;
19cdc75e9fSIngo Molnar
207f192e3cSChristian Brauner /* All the bits taken by the old clone syscall. */
217f192e3cSChristian Brauner #define CLONE_LEGACY_FLAGS 0xffffffffULL
227f192e3cSChristian Brauner
237f192e3cSChristian Brauner struct kernel_clone_args {
247f192e3cSChristian Brauner u64 flags;
257f192e3cSChristian Brauner int __user *pidfd;
267f192e3cSChristian Brauner int __user *child_tid;
277f192e3cSChristian Brauner int __user *parent_tid;
28cf587db2SMike Christie const char *name;
297f192e3cSChristian Brauner int exit_signal;
30c81cc581SMike Christie u32 kthread:1;
31c81cc581SMike Christie u32 io_thread:1;
3254e6842dSMike Christie u32 user_worker:1;
3311f3f500SMike Christie u32 no_files:1;
347f192e3cSChristian Brauner unsigned long stack;
357f192e3cSChristian Brauner unsigned long stack_size;
367f192e3cSChristian Brauner unsigned long tls;
3749cb2fc4SAdrian Reber pid_t *set_tid;
3849cb2fc4SAdrian Reber /* Number of elements in *set_tid */
3949cb2fc4SAdrian Reber size_t set_tid_size;
40ef2c41cfSChristian Brauner int cgroup;
4136cb0e1cSEric W. Biederman int idle;
425bd2e97cSEric W. Biederman int (*fn)(void *);
435bd2e97cSEric W. Biederman void *fn_arg;
44ef2c41cfSChristian Brauner struct cgroup *cgrp;
45ef2c41cfSChristian Brauner struct css_set *cset;
46*b69bb476SShakeel Butt unsigned int kill_seq;
477f192e3cSChristian Brauner };
487f192e3cSChristian Brauner
49901b14bdSIngo Molnar /*
50901b14bdSIngo Molnar * This serializes "schedule()" and also protects
51901b14bdSIngo Molnar * the run-queue from deletions/modifications (but
52901b14bdSIngo Molnar * _adding_ to the beginning of the run-queue has
53901b14bdSIngo Molnar * a separate lock).
54901b14bdSIngo Molnar */
55901b14bdSIngo Molnar extern rwlock_t tasklist_lock;
56901b14bdSIngo Molnar extern spinlock_t mmlist_lock;
57901b14bdSIngo Molnar
58cdc75e9fSIngo Molnar extern union thread_union init_thread_union;
59cdc75e9fSIngo Molnar extern struct task_struct init_task;
60cdc75e9fSIngo Molnar
61901b14bdSIngo Molnar extern int lockdep_tasklist_lock_is_held(void);
62901b14bdSIngo Molnar
63901b14bdSIngo Molnar extern asmlinkage void schedule_tail(struct task_struct *prev);
64901b14bdSIngo Molnar extern void init_idle(struct task_struct *idle, int cpu);
65901b14bdSIngo Molnar
66901b14bdSIngo Molnar extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
67304b3f2bSTejun Heo extern int sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs);
68304b3f2bSTejun Heo extern void sched_cancel_fork(struct task_struct *p);
69b1e82065SPeter Zijlstra extern void sched_post_fork(struct task_struct *p);
70901b14bdSIngo Molnar extern void sched_dead(struct task_struct *p);
71901b14bdSIngo Molnar
72901b14bdSIngo Molnar void __noreturn do_task_dead(void);
730e25498fSEric W. Biederman void __noreturn make_task_dead(int signr);
74901b14bdSIngo Molnar
75af806027SPeter Zijlstra extern void mm_cache_init(void);
76901b14bdSIngo Molnar extern void proc_caches_init(void);
77901b14bdSIngo Molnar
78fb5bf317SYi Wang extern void fork_init(void);
79fb5bf317SYi Wang
80901b14bdSIngo Molnar extern void release_task(struct task_struct * p);
81901b14bdSIngo Molnar
82c5febea0SEric W. Biederman extern int copy_thread(struct task_struct *, const struct kernel_clone_args *);
83901b14bdSIngo Molnar
84901b14bdSIngo Molnar extern void flush_thread(void);
85901b14bdSIngo Molnar
86901b14bdSIngo Molnar #ifdef CONFIG_HAVE_EXIT_THREAD
87901b14bdSIngo Molnar extern void exit_thread(struct task_struct *tsk);
88901b14bdSIngo Molnar #else
exit_thread(struct task_struct * tsk)89901b14bdSIngo Molnar static inline void exit_thread(struct task_struct *tsk)
90901b14bdSIngo Molnar {
91901b14bdSIngo Molnar }
92901b14bdSIngo Molnar #endif
93eae654f1SPeter Zijlstra extern __noreturn void do_group_exit(int);
94901b14bdSIngo Molnar
9542011db0SIngo Molnar extern void exit_files(struct task_struct *);
96d5b36a4dSOleg Nesterov extern void exit_itimers(struct task_struct *);
9742011db0SIngo Molnar
98cad6967aSChristian Brauner extern pid_t kernel_clone(struct kernel_clone_args *kargs);
9989c8e98dSMike Christie struct task_struct *copy_process(struct pid *pid, int trace, int node,
10089c8e98dSMike Christie struct kernel_clone_args *args);
101cc440e87SJens Axboe struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node);
102901b14bdSIngo Molnar struct task_struct *fork_idle(int);
103cf587db2SMike Christie extern pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name,
104cf587db2SMike Christie unsigned long flags);
105343f4c49SEric W. Biederman extern pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags);
106f88a333bSAl Viro extern long kernel_wait4(pid_t, int __user *, int, struct rusage *);
1078043fc14SChristoph Hellwig int kernel_wait(pid_t pid, int *stat);
108901b14bdSIngo Molnar
109cda66725SIngo Molnar extern void free_task(struct task_struct *tsk);
110cda66725SIngo Molnar
1116f175fc9SIngo Molnar /* sched_exec is called by processes performing an exec */
1126f175fc9SIngo Molnar #ifdef CONFIG_SMP
1136f175fc9SIngo Molnar extern void sched_exec(void);
1146f175fc9SIngo Molnar #else
1156f175fc9SIngo Molnar #define sched_exec() {}
1166f175fc9SIngo Molnar #endif
1176f175fc9SIngo Molnar
get_task_struct(struct task_struct * t)1187b3c92b8SMatthew Wilcox (Oracle) static inline struct task_struct *get_task_struct(struct task_struct *t)
1197b3c92b8SMatthew Wilcox (Oracle) {
1207b3c92b8SMatthew Wilcox (Oracle) refcount_inc(&t->usage);
1217b3c92b8SMatthew Wilcox (Oracle) return t;
1227b3c92b8SMatthew Wilcox (Oracle) }
123cda66725SIngo Molnar
tryget_task_struct(struct task_struct * t)124a8532facSTejun Heo static inline struct task_struct *tryget_task_struct(struct task_struct *t)
125a8532facSTejun Heo {
126a8532facSTejun Heo return refcount_inc_not_zero(&t->usage) ? t : NULL;
127a8532facSTejun Heo }
128a8532facSTejun Heo
129cda66725SIngo Molnar extern void __put_task_struct(struct task_struct *t);
130d243b344SWander Lairson Costa extern void __put_task_struct_rcu_cb(struct rcu_head *rhp);
131cda66725SIngo Molnar
put_task_struct(struct task_struct * t)132cda66725SIngo Molnar static inline void put_task_struct(struct task_struct *t)
133cda66725SIngo Molnar {
134d243b344SWander Lairson Costa if (!refcount_dec_and_test(&t->usage))
135d243b344SWander Lairson Costa return;
136d243b344SWander Lairson Costa
137d243b344SWander Lairson Costa /*
138893cdaaaSWander Lairson Costa * In !RT, it is always safe to call __put_task_struct().
139893cdaaaSWander Lairson Costa * Under RT, we can only call it in preemptible context.
140893cdaaaSWander Lairson Costa */
141893cdaaaSWander Lairson Costa if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
142893cdaaaSWander Lairson Costa static DEFINE_WAIT_OVERRIDE_MAP(put_task_map, LD_WAIT_SLEEP);
143893cdaaaSWander Lairson Costa
144893cdaaaSWander Lairson Costa lock_map_acquire_try(&put_task_map);
145893cdaaaSWander Lairson Costa __put_task_struct(t);
146893cdaaaSWander Lairson Costa lock_map_release(&put_task_map);
147893cdaaaSWander Lairson Costa return;
148893cdaaaSWander Lairson Costa }
149893cdaaaSWander Lairson Costa
150893cdaaaSWander Lairson Costa /*
151d243b344SWander Lairson Costa * under PREEMPT_RT, we can't call put_task_struct
152d243b344SWander Lairson Costa * in atomic context because it will indirectly
153d243b344SWander Lairson Costa * acquire sleeping locks.
154d243b344SWander Lairson Costa *
155d243b344SWander Lairson Costa * call_rcu() will schedule delayed_put_task_struct_rcu()
156d243b344SWander Lairson Costa * to be called in process context.
157d243b344SWander Lairson Costa *
158d243b344SWander Lairson Costa * __put_task_struct() is called when
159d243b344SWander Lairson Costa * refcount_dec_and_test(&t->usage) succeeds.
160d243b344SWander Lairson Costa *
161d243b344SWander Lairson Costa * This means that it can't "conflict" with
162d243b344SWander Lairson Costa * put_task_struct_rcu_user() which abuses ->rcu the same
163d243b344SWander Lairson Costa * way; rcu_users has a reference so task->usage can't be
164d243b344SWander Lairson Costa * zero after rcu_users 1 -> 0 transition.
165d243b344SWander Lairson Costa *
166d243b344SWander Lairson Costa * delayed_free_task() also uses ->rcu, but it is only called
167d243b344SWander Lairson Costa * when it fails to fork a process. Therefore, there is no
168d243b344SWander Lairson Costa * way it can conflict with put_task_struct().
169d243b344SWander Lairson Costa */
170d243b344SWander Lairson Costa call_rcu(&t->rcu, __put_task_struct_rcu_cb);
171cda66725SIngo Molnar }
172cda66725SIngo Molnar
DEFINE_FREE(put_task,struct task_struct *,if (_T)put_task_struct (_T))17354da6a09SPeter Zijlstra DEFINE_FREE(put_task, struct task_struct *, if (_T) put_task_struct(_T))
17454da6a09SPeter Zijlstra
175dd6f843aSPavel Begunkov static inline void put_task_struct_many(struct task_struct *t, int nr)
176dd6f843aSPavel Begunkov {
177dd6f843aSPavel Begunkov if (refcount_sub_and_test(nr, &t->usage))
178dd6f843aSPavel Begunkov __put_task_struct(t);
179dd6f843aSPavel Begunkov }
180dd6f843aSPavel Begunkov
1813fbd7ee2SEric W. Biederman void put_task_struct_rcu_user(struct task_struct *task);
182901b14bdSIngo Molnar
1832be9880dSKefeng Wang /* Free all architecture-specific resources held by a thread. */
1842be9880dSKefeng Wang void release_thread(struct task_struct *dead_task);
1852be9880dSKefeng Wang
186901b14bdSIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
187901b14bdSIngo Molnar extern int arch_task_struct_size __read_mostly;
188901b14bdSIngo Molnar #else
189901b14bdSIngo Molnar # define arch_task_struct_size (sizeof(struct task_struct))
190901b14bdSIngo Molnar #endif
191901b14bdSIngo Molnar
1925905429aSKees Cook #ifndef CONFIG_HAVE_ARCH_THREAD_STRUCT_WHITELIST
1935905429aSKees Cook /*
1945905429aSKees Cook * If an architecture has not declared a thread_struct whitelist we
1955905429aSKees Cook * must assume something there may need to be copied to userspace.
1965905429aSKees Cook */
arch_thread_struct_whitelist(unsigned long * offset,unsigned long * size)1975905429aSKees Cook static inline void arch_thread_struct_whitelist(unsigned long *offset,
1985905429aSKees Cook unsigned long *size)
1995905429aSKees Cook {
2005905429aSKees Cook *offset = 0;
2015905429aSKees Cook /* Handle dynamically sized thread_struct. */
2025905429aSKees Cook *size = arch_task_struct_size - offsetof(struct task_struct, thread);
2035905429aSKees Cook }
2045905429aSKees Cook #endif
2055905429aSKees Cook
206901b14bdSIngo Molnar #ifdef CONFIG_VMAP_STACK
task_stack_vm_area(const struct task_struct * t)207901b14bdSIngo Molnar static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
208901b14bdSIngo Molnar {
209901b14bdSIngo Molnar return t->stack_vm_area;
210901b14bdSIngo Molnar }
211901b14bdSIngo Molnar #else
task_stack_vm_area(const struct task_struct * t)212901b14bdSIngo Molnar static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
213901b14bdSIngo Molnar {
214901b14bdSIngo Molnar return NULL;
215901b14bdSIngo Molnar }
216901b14bdSIngo Molnar #endif
217901b14bdSIngo Molnar
21856cd6973SIngo Molnar /*
21956cd6973SIngo Molnar * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
22056cd6973SIngo Molnar * subscriptions and synchronises with wait4(). Also used in procfs. Also
22156cd6973SIngo Molnar * pins the final release of task.io_context. Also protects ->cpuset and
22285b6d246SAlexander Mikhalitsyn * ->cgroup.subsys[]. And ->vfork_done. And ->sysvshm.shm_clist.
22356cd6973SIngo Molnar *
22456cd6973SIngo Molnar * Nests both inside and outside of read_lock(&tasklist_lock).
22556cd6973SIngo Molnar * It must not be nested with write_lock_irq(&tasklist_lock),
22656cd6973SIngo Molnar * neither inside nor outside.
22756cd6973SIngo Molnar */
task_lock(struct task_struct * p)22856cd6973SIngo Molnar static inline void task_lock(struct task_struct *p)
22956cd6973SIngo Molnar {
23056cd6973SIngo Molnar spin_lock(&p->alloc_lock);
23156cd6973SIngo Molnar }
23256cd6973SIngo Molnar
task_unlock(struct task_struct * p)23356cd6973SIngo Molnar static inline void task_unlock(struct task_struct *p)
23456cd6973SIngo Molnar {
23556cd6973SIngo Molnar spin_unlock(&p->alloc_lock);
23656cd6973SIngo Molnar }
23756cd6973SIngo Molnar
2385431fdd2SPeter Zijlstra DEFINE_GUARD(task_lock, struct task_struct *, task_lock(_T), task_unlock(_T))
2395431fdd2SPeter Zijlstra
24029930025SIngo Molnar #endif /* _LINUX_SCHED_TASK_H */
241