pid.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef _LINUX_PID_H
  2. #define _LINUX_PID_H
  3. #include <linux/rcupdate.h>
  4. enum pid_type
  5. {
  6. PIDTYPE_PID,
  7. PIDTYPE_PGID,
  8. PIDTYPE_SID,
  9. PIDTYPE_MAX
  10. };
  11. /*
  12. * What is struct pid?
  13. *
  14. * A struct pid is the kernel's internal notion of a process identifier.
  15. * It refers to individual tasks, process groups, and sessions. While
  16. * there are processes attached to it the struct pid lives in a hash
  17. * table, so it and then the processes that it refers to can be found
  18. * quickly from the numeric pid value. The attached processes may be
  19. * quickly accessed by following pointers from struct pid.
  20. *
  21. * Storing pid_t values in the kernel and refering to them later has a
  22. * problem. The process originally with that pid may have exited and the
  23. * pid allocator wrapped, and another process could have come along
  24. * and been assigned that pid.
  25. *
  26. * Referring to user space processes by holding a reference to struct
  27. * task_struct has a problem. When the user space process exits
  28. * the now useless task_struct is still kept. A task_struct plus a
  29. * stack consumes around 10K of low kernel memory. More precisely
  30. * this is THREAD_SIZE + sizeof(struct task_struct). By comparison
  31. * a struct pid is about 64 bytes.
  32. *
  33. * Holding a reference to struct pid solves both of these problems.
  34. * It is small so holding a reference does not consume a lot of
  35. * resources, and since a new struct pid is allocated when the numeric
  36. * pid value is reused we don't mistakenly refer to new processes.
  37. */
  38. struct pid
  39. {
  40. atomic_t count;
  41. /* Try to keep pid_chain in the same cacheline as nr for find_pid */
  42. int nr;
  43. struct hlist_node pid_chain;
  44. /* lists of tasks that use this pid */
  45. struct hlist_head tasks[PIDTYPE_MAX];
  46. struct rcu_head rcu;
  47. };
  48. struct pid_link
  49. {
  50. struct hlist_node node;
  51. struct pid *pid;
  52. };
  53. static inline struct pid *get_pid(struct pid *pid)
  54. {
  55. if (pid)
  56. atomic_inc(&pid->count);
  57. return pid;
  58. }
  59. extern void FASTCALL(put_pid(struct pid *pid));
  60. extern struct task_struct *FASTCALL(pid_task(struct pid *pid, enum pid_type));
  61. extern struct task_struct *FASTCALL(get_pid_task(struct pid *pid,
  62. enum pid_type));
  63. /*
  64. * attach_pid() and detach_pid() must be called with the tasklist_lock
  65. * write-held.
  66. */
  67. extern int FASTCALL(attach_pid(struct task_struct *task,
  68. enum pid_type type, int nr));
  69. extern void FASTCALL(detach_pid(struct task_struct *task, enum pid_type));
  70. /*
  71. * look up a PID in the hash table. Must be called with the tasklist_lock
  72. * or rcu_read_lock() held.
  73. */
  74. extern struct pid *FASTCALL(find_pid(int nr));
  75. /*
  76. * Lookup a PID in the hash table, and return with it's count elevated.
  77. */
  78. extern struct pid *find_get_pid(int nr);
  79. extern struct pid *alloc_pid(void);
  80. extern void FASTCALL(free_pid(struct pid *pid));
  81. #define pid_next(task, type) \
  82. ((task)->pids[(type)].node.next)
  83. #define pid_next_task(task, type) \
  84. hlist_entry(pid_next(task, type), struct task_struct, \
  85. pids[(type)].node)
  86. /* We could use hlist_for_each_entry_rcu here but it takes more arguments
  87. * than the do_each_task_pid/while_each_task_pid. So we roll our own
  88. * to preserve the existing interface.
  89. */
  90. #define do_each_task_pid(who, type, task) \
  91. if ((task = find_task_by_pid_type(type, who))) { \
  92. prefetch(pid_next(task, type)); \
  93. do {
  94. #define while_each_task_pid(who, type, task) \
  95. } while (pid_next(task, type) && ({ \
  96. task = pid_next_task(task, type); \
  97. rcu_dereference(task); \
  98. prefetch(pid_next(task, type)); \
  99. 1; }) ); \
  100. }
  101. #endif /* _LINUX_PID_H */