pid.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. extern struct pid init_struct_pid;
  50. struct pid_link
  51. {
  52. struct hlist_node node;
  53. struct pid *pid;
  54. };
  55. static inline struct pid *get_pid(struct pid *pid)
  56. {
  57. if (pid)
  58. atomic_inc(&pid->count);
  59. return pid;
  60. }
  61. extern void FASTCALL(put_pid(struct pid *pid));
  62. extern struct task_struct *FASTCALL(pid_task(struct pid *pid, enum pid_type));
  63. extern struct task_struct *FASTCALL(get_pid_task(struct pid *pid,
  64. enum pid_type));
  65. extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
  66. /*
  67. * attach_pid() and detach_pid() must be called with the tasklist_lock
  68. * write-held.
  69. */
  70. extern int FASTCALL(attach_pid(struct task_struct *task,
  71. enum pid_type type, struct pid *pid));
  72. extern void FASTCALL(detach_pid(struct task_struct *task, enum pid_type));
  73. extern void FASTCALL(transfer_pid(struct task_struct *old,
  74. struct task_struct *new, enum pid_type));
  75. /*
  76. * look up a PID in the hash table. Must be called with the tasklist_lock
  77. * or rcu_read_lock() held.
  78. */
  79. extern struct pid *FASTCALL(find_pid(int nr));
  80. /*
  81. * Lookup a PID in the hash table, and return with it's count elevated.
  82. */
  83. extern struct pid *find_get_pid(int nr);
  84. extern struct pid *find_ge_pid(int nr);
  85. extern struct pid *alloc_pid(void);
  86. extern void FASTCALL(free_pid(struct pid *pid));
  87. static inline pid_t pid_nr(struct pid *pid)
  88. {
  89. pid_t nr = 0;
  90. if (pid)
  91. nr = pid->nr;
  92. return nr;
  93. }
  94. #define do_each_pid_task(pid, type, task) \
  95. do { \
  96. struct hlist_node *pos___; \
  97. if (pid != NULL) \
  98. hlist_for_each_entry_rcu((task), pos___, \
  99. &pid->tasks[type], pids[type].node) {
  100. #define while_each_pid_task(pid, type, task) \
  101. } \
  102. } while (0)
  103. #endif /* _LINUX_PID_H */