xref: /linux-6.15/include/linux/pid.h (revision cdefbf23)
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 struct upid {
49 	int nr;
50 	struct pid_namespace *ns;
51 };
52 
53 struct pid
54 {
55 	refcount_t count;
56 	unsigned int level;
57 	spinlock_t lock;
58 	/* lists of tasks that use this pid */
59 	struct hlist_head tasks[PIDTYPE_MAX];
60 	struct hlist_head inodes;
61 	/* wait queue for pidfd notifications */
62 	wait_queue_head_t wait_pidfd;
63 	struct rcu_head rcu;
64 	struct upid numbers[];
65 };
66 
67 extern struct pid init_struct_pid;
68 
69 extern const struct file_operations pidfd_fops;
70 
71 struct file;
72 
73 extern struct pid *pidfd_pid(const struct file *file);
74 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
75 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
76 int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret);
77 
78 static inline struct pid *get_pid(struct pid *pid)
79 {
80 	if (pid)
81 		refcount_inc(&pid->count);
82 	return pid;
83 }
84 
85 extern void put_pid(struct pid *pid);
86 extern struct task_struct *pid_task(struct pid *pid, enum pid_type);
87 static inline bool pid_has_task(struct pid *pid, enum pid_type type)
88 {
89 	return !hlist_empty(&pid->tasks[type]);
90 }
91 extern struct task_struct *get_pid_task(struct pid *pid, enum pid_type);
92 
93 extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
94 
95 /*
96  * these helpers must be called with the tasklist_lock write-held.
97  */
98 extern void attach_pid(struct task_struct *task, enum pid_type);
99 extern void detach_pid(struct task_struct *task, enum pid_type);
100 extern void change_pid(struct task_struct *task, enum pid_type,
101 			struct pid *pid);
102 extern void exchange_tids(struct task_struct *task, struct task_struct *old);
103 extern void transfer_pid(struct task_struct *old, struct task_struct *new,
104 			 enum pid_type);
105 
106 extern int pid_max;
107 extern int pid_max_min, pid_max_max;
108 
109 /*
110  * look up a PID in the hash table. Must be called with the tasklist_lock
111  * or rcu_read_lock() held.
112  *
113  * find_pid_ns() finds the pid in the namespace specified
114  * find_vpid() finds the pid by its virtual id, i.e. in the current namespace
115  *
116  * see also find_task_by_vpid() set in include/linux/sched.h
117  */
118 extern struct pid *find_pid_ns(int nr, struct pid_namespace *ns);
119 extern struct pid *find_vpid(int nr);
120 
121 /*
122  * Lookup a PID in the hash table, and return with it's count elevated.
123  */
124 extern struct pid *find_get_pid(int nr);
125 extern struct pid *find_ge_pid(int nr, struct pid_namespace *);
126 
127 extern struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
128 			     size_t set_tid_size);
129 extern void free_pid(struct pid *pid);
130 extern void disable_pid_allocation(struct pid_namespace *ns);
131 
132 /*
133  * ns_of_pid() returns the pid namespace in which the specified pid was
134  * allocated.
135  *
136  * NOTE:
137  * 	ns_of_pid() is expected to be called for a process (task) that has
138  * 	an attached 'struct pid' (see attach_pid(), detach_pid()) i.e @pid
139  * 	is expected to be non-NULL. If @pid is NULL, caller should handle
140  * 	the resulting NULL pid-ns.
141  */
142 static inline struct pid_namespace *ns_of_pid(struct pid *pid)
143 {
144 	struct pid_namespace *ns = NULL;
145 	if (pid)
146 		ns = pid->numbers[pid->level].ns;
147 	return ns;
148 }
149 
150 /*
151  * is_child_reaper returns true if the pid is the init process
152  * of the current namespace. As this one could be checked before
153  * pid_ns->child_reaper is assigned in copy_process, we check
154  * with the pid number.
155  */
156 static inline bool is_child_reaper(struct pid *pid)
157 {
158 	return pid->numbers[pid->level].nr == 1;
159 }
160 
161 /*
162  * the helpers to get the pid's id seen from different namespaces
163  *
164  * pid_nr()    : global id, i.e. the id seen from the init namespace;
165  * pid_vnr()   : virtual id, i.e. the id seen from the pid namespace of
166  *               current.
167  * pid_nr_ns() : id seen from the ns specified.
168  *
169  * see also task_xid_nr() etc in include/linux/sched.h
170  */
171 
172 static inline pid_t pid_nr(struct pid *pid)
173 {
174 	pid_t nr = 0;
175 	if (pid)
176 		nr = pid->numbers[0].nr;
177 	return nr;
178 }
179 
180 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns);
181 pid_t pid_vnr(struct pid *pid);
182 
183 #define do_each_pid_task(pid, type, task)				\
184 	do {								\
185 		if ((pid) != NULL)					\
186 			hlist_for_each_entry_rcu((task),		\
187 				&(pid)->tasks[type], pid_links[type]) {
188 
189 			/*
190 			 * Both old and new leaders may be attached to
191 			 * the same pid in the middle of de_thread().
192 			 */
193 #define while_each_pid_task(pid, type, task)				\
194 				if (type == PIDTYPE_PID)		\
195 					break;				\
196 			}						\
197 	} while (0)
198 
199 #define do_each_pid_thread(pid, type, task)				\
200 	do_each_pid_task(pid, type, task) {				\
201 		struct task_struct *tg___ = task;			\
202 		for_each_thread(tg___, task) {
203 
204 #define while_each_pid_thread(pid, type, task)				\
205 		}							\
206 		task = tg___;						\
207 	} while_each_pid_task(pid, type, task)
208 
209 static inline struct pid *task_pid(struct task_struct *task)
210 {
211 	return task->thread_pid;
212 }
213 
214 /*
215  * the helpers to get the task's different pids as they are seen
216  * from various namespaces
217  *
218  * task_xid_nr()     : global id, i.e. the id seen from the init namespace;
219  * task_xid_vnr()    : virtual id, i.e. the id seen from the pid namespace of
220  *                     current.
221  * task_xid_nr_ns()  : id seen from the ns specified;
222  *
223  * see also pid_nr() etc in include/linux/pid.h
224  */
225 pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
226 
227 static inline pid_t task_pid_nr(struct task_struct *tsk)
228 {
229 	return tsk->pid;
230 }
231 
232 static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
233 {
234 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
235 }
236 
237 static inline pid_t task_pid_vnr(struct task_struct *tsk)
238 {
239 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
240 }
241 
242 
243 static inline pid_t task_tgid_nr(struct task_struct *tsk)
244 {
245 	return tsk->tgid;
246 }
247 
248 /**
249  * pid_alive - check that a task structure is not stale
250  * @p: Task structure to be checked.
251  *
252  * Test if a process is not yet dead (at most zombie state)
253  * If pid_alive fails, then pointers within the task structure
254  * can be stale and must not be dereferenced.
255  *
256  * Return: 1 if the process is alive. 0 otherwise.
257  */
258 static inline int pid_alive(const struct task_struct *p)
259 {
260 	return p->thread_pid != NULL;
261 }
262 
263 static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
264 {
265 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
266 }
267 
268 static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
269 {
270 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
271 }
272 
273 
274 static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
275 {
276 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
277 }
278 
279 static inline pid_t task_session_vnr(struct task_struct *tsk)
280 {
281 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
282 }
283 
284 static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
285 {
286 	return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
287 }
288 
289 static inline pid_t task_tgid_vnr(struct task_struct *tsk)
290 {
291 	return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL);
292 }
293 
294 static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
295 {
296 	pid_t pid = 0;
297 
298 	rcu_read_lock();
299 	if (pid_alive(tsk))
300 		pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
301 	rcu_read_unlock();
302 
303 	return pid;
304 }
305 
306 static inline pid_t task_ppid_nr(const struct task_struct *tsk)
307 {
308 	return task_ppid_nr_ns(tsk, &init_pid_ns);
309 }
310 
311 /* Obsolete, do not use: */
312 static inline pid_t task_pgrp_nr(struct task_struct *tsk)
313 {
314 	return task_pgrp_nr_ns(tsk, &init_pid_ns);
315 }
316 
317 /**
318  * is_global_init - check if a task structure is init. Since init
319  * is free to have sub-threads we need to check tgid.
320  * @tsk: Task structure to be checked.
321  *
322  * Check if a task structure is the first user space task the kernel created.
323  *
324  * Return: 1 if the task structure is init. 0 otherwise.
325  */
326 static inline int is_global_init(struct task_struct *tsk)
327 {
328 	return task_tgid_nr(tsk) == 1;
329 }
330 
331 #endif /* _LINUX_PID_H */
332