pid.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. extern void FASTCALL(transfer_pid(struct task_struct *old,
  71. struct task_struct *new, enum pid_type));
  72. /*
  73. * look up a PID in the hash table. Must be called with the tasklist_lock
  74. * or rcu_read_lock() held.
  75. */
  76. extern struct pid *FASTCALL(find_pid(int nr));
  77. /*
  78. * Lookup a PID in the hash table, and return with it's count elevated.
  79. */
  80. extern struct pid *find_get_pid(int nr);
  81. extern struct pid *find_ge_pid(int nr);
  82. extern struct pid *alloc_pid(void);
  83. extern void FASTCALL(free_pid(struct pid *pid));
  84. #define pid_next(task, type) \
  85. ((task)->pids[(type)].node.next)
  86. #define pid_next_task(task, type) \
  87. hlist_entry(pid_next(task, type), struct task_struct, \
  88. pids[(type)].node)
  89. /* We could use hlist_for_each_entry_rcu here but it takes more arguments
  90. * than the do_each_task_pid/while_each_task_pid. So we roll our own
  91. * to preserve the existing interface.
  92. */
  93. #define do_each_task_pid(who, type, task) \
  94. if ((task = find_task_by_pid_type(type, who))) { \
  95. prefetch(pid_next(task, type)); \
  96. do {
  97. #define while_each_task_pid(who, type, task) \
  98. } while (pid_next(task, type) && ({ \
  99. task = pid_next_task(task, type); \
  100. rcu_dereference(task); \
  101. prefetch(pid_next(task, type)); \
  102. 1; }) ); \
  103. }
  104. #define do_each_pid_task(pid, type, task) \
  105. if ((task = pid_task(pid, type))) { \
  106. prefetch(pid_next(task, type)); \
  107. do {
  108. #define while_each_pid_task(pid, type, task) \
  109. } while (pid_next(task, type) && ({ \
  110. task = pid_next_task(task, type); \
  111. rcu_dereference(task); \
  112. prefetch(pid_next(task, type)); \
  113. 1; }) ); \
  114. }
  115. #endif /* _LINUX_PID_H */