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