Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / include / linux / pid.h
1 #ifndef _LINUX_PID_H
2 #define _LINUX_PID_H
3
4 #include <linux/rcupdate.h>
5
6 enum pid_type
7 {
8         PIDTYPE_PID,
9         PIDTYPE_PGID,
10         PIDTYPE_SID,
11         PIDTYPE_MAX,
12         PIDTYPE_REALPID
13 };
14
15 /*
16  * What is struct pid?
17  *
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.
24  *
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.
29  *
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.
36  *
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.
41  */
42
43 struct pid
44 {
45         atomic_t count;
46         /* Try to keep pid_chain in the same cacheline as nr for find_pid */
47         int nr;
48         struct hlist_node pid_chain;
49         /* lists of tasks that use this pid */
50         struct hlist_head tasks[PIDTYPE_MAX];
51         struct rcu_head rcu;
52 };
53
54 struct pid_link
55 {
56         struct hlist_node node;
57         struct pid *pid;
58 };
59
60 static inline struct pid *get_pid(struct pid *pid)
61 {
62         if (pid)
63                 atomic_inc(&pid->count);
64         return pid;
65 }
66
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,
70                                                 enum pid_type));
71
72 /*
73  * attach_pid() and detach_pid() must be called with the tasklist_lock
74  * write-held.
75  */
76 extern int FASTCALL(attach_pid(struct task_struct *task,
77                                 enum pid_type type, int nr));
78
79 extern void FASTCALL(detach_pid(struct task_struct *task, enum pid_type));
80
81 /*
82  * look up a PID in the hash table. Must be called with the tasklist_lock
83  * or rcu_read_lock() held.
84  */
85 extern struct pid *FASTCALL(find_pid(int nr));
86
87 /*
88  * Lookup a PID in the hash table, and return with it's count elevated.
89  */
90 extern struct pid *find_get_pid(int nr);
91
92 extern struct pid *alloc_pid(void);
93 extern void FASTCALL(free_pid(struct pid *pid));
94
95 #define pid_next(task, type)                                    \
96         ((task)->pids[(type)].node.next)
97
98 #define pid_next_task(task, type)                               \
99         hlist_entry(pid_next(task, type), struct task_struct,   \
100                         pids[(type)].node)
101
102
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.
106  */
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));                         \
110                 do {
111
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));         \
117                                 1; }) );                                \
118         }
119
120 #endif /* _LINUX_PID_H */