1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PID_H
3 #define _LINUX_PID_H
4
5 #include <linux/array_size.h>
6 #include <linux/build_bug.h>
7 #include <linux/minmax.h>
8 #include <linux/pid_types.h>
9 #include <linux/rculist.h>
10 #include <linux/rcupdate.h>
11 #include <linux/refcount.h>
12 #include <linux/rhashtable-types.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15
16 /*
17 * What is struct pid?
18 *
19 * A struct pid is the kernel's internal notion of a process identifier.
20 * It refers to individual tasks, process groups, and sessions. While
21 * there are processes attached to it the struct pid lives in a hash
22 * table, so it and then the processes that it refers to can be found
23 * quickly from the numeric pid value. The attached processes may be
24 * quickly accessed by following pointers from struct pid.
25 *
26 * Storing pid_t values in the kernel and referring to them later has a
27 * problem. The process originally with that pid may have exited and the
28 * pid allocator wrapped, and another process could have come along
29 * and been assigned that pid.
30 *
31 * Referring to user space processes by holding a reference to struct
32 * task_struct has a problem. When the user space process exits
33 * the now useless task_struct is still kept. A task_struct plus a
34 * stack consumes around 10K of low kernel memory. More precisely
35 * this is THREAD_SIZE + sizeof(struct task_struct). By comparison
36 * a struct pid is about 64 bytes.
37 *
38 * Holding a reference to struct pid solves both of these problems.
39 * It is small so holding a reference does not consume a lot of
40 * resources, and since a new struct pid is allocated when the numeric pid
41 * value is reused (when pids wrap around) we don't mistakenly refer to new
42 * processes.
43 */
44
45
46 /*
47 * struct upid is used to get the id of the struct pid, as it is
48 * seen in particular namespace. Later the struct pid is found with
49 * find_pid_ns() using the int nr and struct pid_namespace *ns.
50 */
51
52 #define RESERVED_PIDS 300
53
54 struct pidfs_attr;
55
56 struct upid {
57 int nr;
58 struct pid_namespace *ns;
59 };
60
61 struct pid {
62 refcount_t count;
63 unsigned int level;
64 spinlock_t lock;
65 struct {
66 u64 ino;
67 struct rhash_head pidfs_hash;
68 struct dentry *stashed;
69 struct pidfs_attr *attr;
70 };
71 /* lists of tasks that use this pid */
72 struct hlist_head tasks[PIDTYPE_MAX];
73 struct hlist_head inodes;
74 /* wait queue for pidfd notifications */
75 wait_queue_head_t wait_pidfd;
76 struct rcu_head rcu;
77 struct upid numbers[];
78 };
79
80 extern struct pid init_struct_pid;
81
82 struct file;
83
84 struct pid *pidfd_pid(const struct file *file);
85 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
86 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
87 int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file);
88 void do_notify_pidfd(struct task_struct *task);
89
get_pid(struct pid * pid)90 static inline struct pid *get_pid(struct pid *pid)
91 {
92 if (pid)
93 refcount_inc(&pid->count);
94 return pid;
95 }
96
97 extern void put_pid(struct pid *pid);
98
99 /*
100 * Helpers for arrays of struct pid indexed by pid type declared with
101 * DECLARE_PIDS(). The array covers PIDTYPE_PID up to the pid type it
102 * was declared with and the helpers take that bound from the array.
103 */
__get_pids(struct pid ** dst,struct pid * const * src,enum pid_type last)104 static inline void __get_pids(struct pid **dst, struct pid *const *src,
105 enum pid_type last)
106 {
107 for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
108 dst[type] = get_pid(src[type]);
109 }
110
__put_pids(struct pid ** pids,enum pid_type last)111 static inline void __put_pids(struct pid **pids, enum pid_type last)
112 {
113 for (enum pid_type type = PIDTYPE_PID; type <= last; type++) {
114 put_pid(pids[type]);
115 pids[type] = NULL;
116 }
117 }
118
__swap_pids(struct pid ** a,struct pid ** b,enum pid_type last)119 static inline void __swap_pids(struct pid **a, struct pid **b,
120 enum pid_type last)
121 {
122 for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
123 swap(a[type], b[type]);
124 }
125
__pids_equal(struct pid * const * a,struct pid * const * b,enum pid_type last)126 static inline bool __pids_equal(struct pid *const *a, struct pid *const *b,
127 enum pid_type last)
128 {
129 for (enum pid_type type = PIDTYPE_PID; type <= last; type++)
130 if (a[type] != b[type])
131 return false;
132 return true;
133 }
134
135 /* The last pid type an array declared with DECLARE_PIDS() covers. */
136 #define pids_last(pids) \
137 ((enum pid_type)(ARRAY_SIZE(pids) - 1 + \
138 BUILD_BUG_ON_ZERO(ARRAY_SIZE(pids) > PIDTYPE_MAX)))
139
140 #define __pids_last2(a, b) \
141 (pids_last(a) + BUILD_BUG_ON_ZERO(ARRAY_SIZE(a) != ARRAY_SIZE(b)))
142
143 #define get_pids(dst, src) __get_pids(dst, src, __pids_last2(dst, src))
144 #define put_pids(pids) __put_pids(pids, pids_last(pids))
145 #define swap_pids(a, b) __swap_pids(a, b, __pids_last2(a, b))
146 #define pids_equal(a, b) __pids_equal(a, b, __pids_last2(a, b))
147
148 extern struct task_struct *pid_task(struct pid *pid, enum pid_type);
pid_has_task(struct pid * pid,enum pid_type type)149 static inline bool pid_has_task(struct pid *pid, enum pid_type type)
150 {
151 return !hlist_empty(&pid->tasks[type]);
152 }
153 extern struct task_struct *get_pid_task(struct pid *pid, enum pid_type);
154
155 extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
156
157 /*
158 * these helpers must be called with the tasklist_lock write-held.
159 */
160 extern void attach_pid(struct task_struct *task, enum pid_type);
161 void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type);
162 void change_pid(struct pid **pids, struct task_struct *task, enum pid_type,
163 struct pid *pid);
164 extern void exchange_tids(struct task_struct *task, struct task_struct *old);
165 extern void transfer_pid(struct task_struct *old, struct task_struct *new,
166 enum pid_type);
167
168 /*
169 * look up a PID in the hash table. Must be called with the tasklist_lock
170 * or rcu_read_lock() held.
171 *
172 * find_pid_ns() finds the pid in the namespace specified
173 * find_vpid() finds the pid by its virtual id, i.e. in the current namespace
174 *
175 * see also find_task_by_vpid() set in include/linux/sched.h
176 */
177 extern struct pid *find_pid_ns(int nr, struct pid_namespace *ns);
178 extern struct pid *find_vpid(int nr);
179
180 /*
181 * Lookup a PID in the hash table, and return with it's count elevated.
182 */
183 extern struct pid *find_get_pid(int nr);
184 extern struct pid *find_ge_pid(int nr, struct pid_namespace *);
185
186 extern struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
187 size_t set_tid_size);
188 extern void free_pid(struct pid *pid);
189 void free_pids(struct pid **pids);
190 extern void disable_pid_allocation(struct pid_namespace *ns);
191
192 /*
193 * ns_of_pid() returns the pid namespace in which the specified pid was
194 * allocated.
195 *
196 * NOTE:
197 * ns_of_pid() is expected to be called for a process (task) that has
198 * an attached 'struct pid' (see attach_pid(), detach_pid()) i.e @pid
199 * is expected to be non-NULL. If @pid is NULL, caller should handle
200 * the resulting NULL pid-ns.
201 */
ns_of_pid(struct pid * pid)202 static inline struct pid_namespace *ns_of_pid(struct pid *pid)
203 {
204 struct pid_namespace *ns = NULL;
205 if (pid)
206 ns = pid->numbers[pid->level].ns;
207 return ns;
208 }
209
210 /*
211 * is_child_reaper returns true if the pid is the init process
212 * of the current namespace. As this one could be checked before
213 * pid_ns->child_reaper is assigned in copy_process, we check
214 * with the pid number.
215 */
is_child_reaper(struct pid * pid)216 static inline bool is_child_reaper(struct pid *pid)
217 {
218 return pid->numbers[pid->level].nr == 1;
219 }
220
221 /*
222 * the helpers to get the pid's id seen from different namespaces
223 *
224 * pid_nr() : global id, i.e. the id seen from the init namespace;
225 * pid_vnr() : virtual id, i.e. the id seen from the pid namespace of
226 * current.
227 * pid_nr_ns() : id seen from the ns specified.
228 *
229 * see also task_xid_nr() etc in include/linux/sched.h
230 */
231
pid_nr(struct pid * pid)232 static inline pid_t pid_nr(struct pid *pid)
233 {
234 pid_t nr = 0;
235 if (pid)
236 nr = pid->numbers[0].nr;
237 return nr;
238 }
239
240 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns);
241 pid_t pid_vnr(struct pid *pid);
242
243 /*
244 * Needs tasklist_lock if type > PIDTYPE_TGID. A lockless reader can:
245 *
246 * - race with a change_pid() that moves the task to another hlist,
247 * and continue to scan the wrong list.
248 *
249 * - race with a copy_process() that adds a new task to the start of hlist,
250 * and miss the new child.
251 */
252 #define do_each_pid_task(pid, type, task) \
253 do { \
254 if ((pid) != NULL) \
255 hlist_for_each_entry_rcu((task), \
256 &(pid)->tasks[type], pid_links[type]) {
257
258 /*
259 * Both old and new leaders may be attached to
260 * the same pid in the middle of de_thread().
261 */
262 #define while_each_pid_task(pid, type, task) \
263 if (type == PIDTYPE_PID) \
264 break; \
265 } \
266 } while (0)
267
268 #define do_each_pid_thread(pid, type, task) \
269 do_each_pid_task(pid, type, task) { \
270 struct task_struct *tg___ = task; \
271 for_each_thread(tg___, task) {
272
273 #define while_each_pid_thread(pid, type, task) \
274 } \
275 task = tg___; \
276 } while_each_pid_task(pid, type, task)
277
task_pid(struct task_struct * task)278 static inline struct pid *task_pid(struct task_struct *task)
279 {
280 return task->thread_pid;
281 }
282
283 /*
284 * the helpers to get the task's different pids as they are seen
285 * from various namespaces
286 *
287 * task_xid_nr() : global id, i.e. the id seen from the init namespace;
288 * task_xid_vnr() : virtual id, i.e. the id seen from the pid namespace of
289 * current.
290 * task_xid_nr_ns() : id seen from the ns specified;
291 *
292 * see also pid_nr() etc in include/linux/pid.h
293 */
294 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
295
task_pid_nr(struct task_struct * tsk)296 static inline pid_t task_pid_nr(struct task_struct *tsk)
297 {
298 return tsk->pid;
299 }
300
task_pid_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)301 static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
302 {
303 return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
304 }
305
task_pid_vnr(struct task_struct * tsk)306 static inline pid_t task_pid_vnr(struct task_struct *tsk)
307 {
308 return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
309 }
310
311
task_tgid_nr(struct task_struct * tsk)312 static inline pid_t task_tgid_nr(struct task_struct *tsk)
313 {
314 return tsk->tgid;
315 }
316
317 /**
318 * pid_alive - check that a task structure is not stale
319 * @p: Task structure to be checked.
320 *
321 * Test if a process is not yet dead (at most zombie state)
322 * If pid_alive fails, then pointers within the task structure
323 * can be stale and must not be dereferenced.
324 *
325 * Return: 1 if the process is alive. 0 otherwise.
326 */
pid_alive(const struct task_struct * p)327 static inline int pid_alive(const struct task_struct *p)
328 {
329 return READ_ONCE(p->thread_pid) != NULL;
330 }
331
task_pgrp_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)332 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
333 {
334 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
335 }
336
task_pgrp_vnr(struct task_struct * tsk)337 static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
338 {
339 return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
340 }
341
342
task_session_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)343 static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
344 {
345 return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
346 }
347
task_session_vnr(struct task_struct * tsk)348 static inline pid_t task_session_vnr(struct task_struct *tsk)
349 {
350 return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
351 }
352
task_tgid_nr_ns(struct task_struct * tsk,struct pid_namespace * ns)353 static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
354 {
355 return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
356 }
357
task_tgid_vnr(struct task_struct * tsk)358 static inline pid_t task_tgid_vnr(struct task_struct *tsk)
359 {
360 return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL);
361 }
362
task_ppid_nr_ns(const struct task_struct * tsk,struct pid_namespace * ns)363 static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
364 {
365 pid_t pid = 0;
366
367 rcu_read_lock();
368 if (pid_alive(tsk))
369 pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
370 rcu_read_unlock();
371
372 return pid;
373 }
374
task_ppid_vnr(const struct task_struct * tsk)375 static inline pid_t task_ppid_vnr(const struct task_struct *tsk)
376 {
377 return task_ppid_nr_ns(tsk, NULL);
378 }
379
task_ppid_nr(const struct task_struct * tsk)380 static inline pid_t task_ppid_nr(const struct task_struct *tsk)
381 {
382 return task_ppid_nr_ns(tsk, &init_pid_ns);
383 }
384
385 /* Obsolete, do not use: */
task_pgrp_nr(struct task_struct * tsk)386 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
387 {
388 return task_pgrp_nr_ns(tsk, &init_pid_ns);
389 }
390
391 /**
392 * is_global_init - check if a task structure is init. Since init
393 * is free to have sub-threads we need to check tgid.
394 * @tsk: Task structure to be checked.
395 *
396 * Check if a task structure is the first user space task the kernel created.
397 *
398 * Return: 1 if the task structure is init. 0 otherwise.
399 */
is_global_init(struct task_struct * tsk)400 static inline int is_global_init(struct task_struct *tsk)
401 {
402 return task_tgid_nr(tsk) == 1;
403 }
404
405 #endif /* _LINUX_PID_H */
406