pid.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
  64. /*
  65. * attach_pid() and detach_pid() must be called with the tasklist_lock
  66. * write-held.
  67. */
  68. extern int FASTCALL(attach_pid(struct task_struct *task,
  69. enum pid_type type, int nr));
  70. extern void FASTCALL(detach_pid(struct task_struct *task, enum pid_type));
  71. extern void FASTCALL(transfer_pid(struct task_struct *old,
  72. struct task_struct *new, enum pid_type));
  73. /*
  74. * look up a PID in the hash table. Must be called with the tasklist_lock
  75. * or rcu_read_lock() held.
  76. */
  77. extern struct pid *FASTCALL(find_pid(int nr));
  78. /*
  79. * Lookup a PID in the hash table, and return with it's count elevated.
  80. */
  81. extern struct pid *find_get_pid(int nr);
  82. extern struct pid *find_ge_pid(int nr);
  83. extern struct pid *alloc_pid(void);
  84. extern void FASTCALL(free_pid(struct pid *pid));
  85. static inline pid_t pid_nr(struct pid *pid)
  86. {
  87. pid_t nr = 0;
  88. if (pid)
  89. nr = pid->nr;
  90. return nr;
  91. }
  92. #define do_each_task_pid(who, type, task) \
  93. do { \
  94. struct hlist_node *pos___; \
  95. struct pid *pid___ = find_pid(who); \
  96. if (pid___ != NULL) \
  97. hlist_for_each_entry_rcu((task), pos___, \
  98. &pid___->tasks[type], pids[type].node) {
  99. #define while_each_task_pid(who, type, task) \
  100. } \
  101. } while (0)
  102. #define do_each_pid_task(pid, type, task) \
  103. do { \
  104. struct hlist_node *pos___; \
  105. if (pid != NULL) \
  106. hlist_for_each_entry_rcu((task), pos___, \
  107. &pid->tasks[type], pids[type].node) {
  108. #define while_each_pid_task(pid, type, task) \
  109. } \
  110. } while (0)
  111. #endif /* _LINUX_PID_H */