pid.h 3.3 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 pid
  36. * value is reused (when pids wrap around) we don't mistakenly refer to new
  37. * processes.
  38. */
  39. struct pid
  40. {
  41. atomic_t count;
  42. /* Try to keep pid_chain in the same cacheline as nr for find_pid */
  43. int nr;
  44. struct hlist_node pid_chain;
  45. /* lists of tasks that use this pid */
  46. struct hlist_head tasks[PIDTYPE_MAX];
  47. struct rcu_head rcu;
  48. };
  49. struct pid_link
  50. {
  51. struct hlist_node node;
  52. struct pid *pid;
  53. };
  54. static inline struct pid *get_pid(struct pid *pid)
  55. {
  56. if (pid)
  57. atomic_inc(&pid->count);
  58. return pid;
  59. }
  60. extern void FASTCALL(put_pid(struct pid *pid));
  61. extern struct task_struct *FASTCALL(pid_task(struct pid *pid, enum pid_type));
  62. extern struct task_struct *FASTCALL(get_pid_task(struct pid *pid,
  63. enum pid_type));
  64. extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
  65. /*
  66. * attach_pid() and detach_pid() must be called with the tasklist_lock
  67. * write-held.
  68. */
  69. extern int FASTCALL(attach_pid(struct task_struct *task,
  70. enum pid_type type, int nr));
  71. extern void FASTCALL(detach_pid(struct task_struct *task, enum pid_type));
  72. extern void FASTCALL(transfer_pid(struct task_struct *old,
  73. struct task_struct *new, enum pid_type));
  74. /*
  75. * look up a PID in the hash table. Must be called with the tasklist_lock
  76. * or rcu_read_lock() held.
  77. */
  78. extern struct pid *FASTCALL(find_pid(int nr));
  79. /*
  80. * Lookup a PID in the hash table, and return with it's count elevated.
  81. */
  82. extern struct pid *find_get_pid(int nr);
  83. extern struct pid *find_ge_pid(int nr);
  84. extern struct pid *alloc_pid(void);
  85. extern void FASTCALL(free_pid(struct pid *pid));
  86. static inline pid_t pid_nr(struct pid *pid)
  87. {
  88. pid_t nr = 0;
  89. if (pid)
  90. nr = pid->nr;
  91. return nr;
  92. }
  93. #define do_each_pid_task(pid, type, task) \
  94. do { \
  95. struct hlist_node *pos___; \
  96. if (pid != NULL) \
  97. hlist_for_each_entry_rcu((task), pos___, \
  98. &pid->tasks[type], pids[type].node) {
  99. #define while_each_pid_task(pid, type, task) \
  100. } \
  101. } while (0)
  102. #endif /* _LINUX_PID_H */