4 #include <linux/rcupdate.h>
18 * A struct pid is the kernel's internal notion of a process identifier.
19 * It refers to individual tasks, process groups, and sessions. While
20 * there are processes attached to it the struct pid lives in a hash
21 * table, so it and then the processes that it refers to can be found
22 * quickly from the numeric pid value. The attached processes may be
23 * quickly accessed by following pointers from struct pid.
25 * Storing pid_t values in the kernel and refering to them later has a
26 * problem. The process originally with that pid may have exited and the
27 * pid allocator wrapped, and another process could have come along
28 * and been assigned that pid.
30 * Referring to user space processes by holding a reference to struct
31 * task_struct has a problem. When the user space process exits
32 * the now useless task_struct is still kept. A task_struct plus a
33 * stack consumes around 10K of low kernel memory. More precisely
34 * this is THREAD_SIZE + sizeof(struct task_struct). By comparison
35 * a struct pid is about 64 bytes.
37 * Holding a reference to struct pid solves both of these problems.
38 * It is small so holding a reference does not consume a lot of
39 * resources, and since a new struct pid is allocated when the numeric
40 * pid value is reused we don't mistakenly refer to new processes.
46 /* Try to keep pid_chain in the same cacheline as nr for find_pid */
48 struct hlist_node pid_chain;
49 /* lists of tasks that use this pid */
50 struct hlist_head tasks[PIDTYPE_MAX];
56 struct hlist_node node;
60 static inline struct pid *get_pid(struct pid *pid)
63 atomic_inc(&pid->count);
67 extern void FASTCALL(put_pid(struct pid *pid));
68 extern struct task_struct *FASTCALL(pid_task(struct pid *pid, enum pid_type));
69 extern struct task_struct *FASTCALL(get_pid_task(struct pid *pid,
73 * attach_pid() and detach_pid() must be called with the tasklist_lock
76 extern int FASTCALL(attach_pid(struct task_struct *task,
77 enum pid_type type, int nr));
79 extern void FASTCALL(detach_pid(struct task_struct *task, enum pid_type));
82 * look up a PID in the hash table. Must be called with the tasklist_lock
83 * or rcu_read_lock() held.
85 extern struct pid *FASTCALL(find_pid(int nr));
88 * Lookup a PID in the hash table, and return with it's count elevated.
90 extern struct pid *find_get_pid(int nr);
92 extern struct pid *alloc_pid(void);
93 extern void FASTCALL(free_pid(struct pid *pid));
95 #define pid_next(task, type) \
96 ((task)->pids[(type)].node.next)
98 #define pid_next_task(task, type) \
99 hlist_entry(pid_next(task, type), struct task_struct, \
103 /* We could use hlist_for_each_entry_rcu here but it takes more arguments
104 * than the do_each_task_pid/while_each_task_pid. So we roll our own
105 * to preserve the existing interface.
107 #define do_each_task_pid(who, type, task) \
108 if ((task = find_task_by_pid_type(type, who))) { \
109 prefetch(pid_next(task, type)); \
112 #define while_each_task_pid(who, type, task) \
113 } while (pid_next(task, type) && ({ \
114 task = pid_next_task(task, type); \
115 rcu_dereference(task); \
116 prefetch(pid_next(task, type)); \
120 #endif /* _LINUX_PID_H */