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