cgroup.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef _LINUX_CGROUP_H
  2. #define _LINUX_CGROUP_H
  3. /*
  4. * cgroup interface
  5. *
  6. * Copyright (C) 2003 BULL SA
  7. * Copyright (C) 2004-2006 Silicon Graphics, Inc.
  8. *
  9. */
  10. #include <linux/sched.h>
  11. #include <linux/kref.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/rcupdate.h>
  15. #ifdef CONFIG_CGROUPS
  16. struct cgroupfs_root;
  17. struct cgroup_subsys;
  18. struct inode;
  19. extern int cgroup_init_early(void);
  20. extern int cgroup_init(void);
  21. extern void cgroup_init_smp(void);
  22. extern void cgroup_lock(void);
  23. extern void cgroup_unlock(void);
  24. extern void cgroup_fork(struct task_struct *p);
  25. extern void cgroup_fork_callbacks(struct task_struct *p);
  26. extern void cgroup_exit(struct task_struct *p, int run_callbacks);
  27. extern struct file_operations proc_cgroup_operations;
  28. /* Per-subsystem/per-cgroup state maintained by the system. */
  29. struct cgroup_subsys_state {
  30. /* The cgroup that this subsystem is attached to. Useful
  31. * for subsystems that want to know about the cgroup
  32. * hierarchy structure */
  33. struct cgroup *cgroup;
  34. /* State maintained by the cgroup system to allow
  35. * subsystems to be "busy". Should be accessed via css_get()
  36. * and css_put() */
  37. atomic_t refcnt;
  38. unsigned long flags;
  39. };
  40. /* bits in struct cgroup_subsys_state flags field */
  41. enum {
  42. CSS_ROOT, /* This CSS is the root of the subsystem */
  43. };
  44. /*
  45. * Call css_get() to hold a reference on the cgroup;
  46. *
  47. */
  48. static inline void css_get(struct cgroup_subsys_state *css)
  49. {
  50. /* We don't need to reference count the root state */
  51. if (!test_bit(CSS_ROOT, &css->flags))
  52. atomic_inc(&css->refcnt);
  53. }
  54. /*
  55. * css_put() should be called to release a reference taken by
  56. * css_get()
  57. */
  58. static inline void css_put(struct cgroup_subsys_state *css)
  59. {
  60. if (!test_bit(CSS_ROOT, &css->flags))
  61. atomic_dec(&css->refcnt);
  62. }
  63. struct cgroup {
  64. unsigned long flags; /* "unsigned long" so bitops work */
  65. /* count users of this cgroup. >0 means busy, but doesn't
  66. * necessarily indicate the number of tasks in the
  67. * cgroup */
  68. atomic_t count;
  69. /*
  70. * We link our 'sibling' struct into our parent's 'children'.
  71. * Our children link their 'sibling' into our 'children'.
  72. */
  73. struct list_head sibling; /* my parent's children */
  74. struct list_head children; /* my children */
  75. struct cgroup *parent; /* my parent */
  76. struct dentry *dentry; /* cgroup fs entry */
  77. /* Private pointers for each registered subsystem */
  78. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  79. struct cgroupfs_root *root;
  80. struct cgroup *top_cgroup;
  81. };
  82. /* struct cftype:
  83. *
  84. * The files in the cgroup filesystem mostly have a very simple read/write
  85. * handling, some common function will take care of it. Nevertheless some cases
  86. * (read tasks) are special and therefore I define this structure for every
  87. * kind of file.
  88. *
  89. *
  90. * When reading/writing to a file:
  91. * - the cgroup to use in file->f_dentry->d_parent->d_fsdata
  92. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  93. */
  94. #define MAX_CFTYPE_NAME 64
  95. struct cftype {
  96. /* By convention, the name should begin with the name of the
  97. * subsystem, followed by a period */
  98. char name[MAX_CFTYPE_NAME];
  99. int private;
  100. int (*open) (struct inode *inode, struct file *file);
  101. ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
  102. struct file *file,
  103. char __user *buf, size_t nbytes, loff_t *ppos);
  104. /*
  105. * read_uint() is a shortcut for the common case of returning a
  106. * single integer. Use it in place of read()
  107. */
  108. u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
  109. ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
  110. struct file *file,
  111. const char __user *buf, size_t nbytes, loff_t *ppos);
  112. /*
  113. * write_uint() is a shortcut for the common case of accepting
  114. * a single integer (as parsed by simple_strtoull) from
  115. * userspace. Use in place of write(); return 0 or error.
  116. */
  117. int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
  118. int (*release) (struct inode *inode, struct file *file);
  119. };
  120. /* Add a new file to the given cgroup directory. Should only be
  121. * called by subsystems from within a populate() method */
  122. int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
  123. const struct cftype *cft);
  124. /* Add a set of new files to the given cgroup directory. Should
  125. * only be called by subsystems from within a populate() method */
  126. int cgroup_add_files(struct cgroup *cont,
  127. struct cgroup_subsys *subsys,
  128. const struct cftype cft[],
  129. int count);
  130. int cgroup_is_removed(const struct cgroup *cont);
  131. int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
  132. int __cgroup_task_count(const struct cgroup *cont);
  133. static inline int cgroup_task_count(const struct cgroup *cont)
  134. {
  135. int task_count;
  136. rcu_read_lock();
  137. task_count = __cgroup_task_count(cont);
  138. rcu_read_unlock();
  139. return task_count;
  140. }
  141. /* Return true if the cgroup is a descendant of the current cgroup */
  142. int cgroup_is_descendant(const struct cgroup *cont);
  143. /* Control Group subsystem type. See Documentation/cgroups.txt for details */
  144. struct cgroup_subsys {
  145. struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
  146. struct cgroup *cont);
  147. void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
  148. int (*can_attach)(struct cgroup_subsys *ss,
  149. struct cgroup *cont, struct task_struct *tsk);
  150. void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
  151. struct cgroup *old_cont, struct task_struct *tsk);
  152. void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
  153. void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
  154. int (*populate)(struct cgroup_subsys *ss,
  155. struct cgroup *cont);
  156. void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
  157. void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
  158. int subsys_id;
  159. int active;
  160. int early_init;
  161. #define MAX_CGROUP_TYPE_NAMELEN 32
  162. const char *name;
  163. /* Protected by RCU */
  164. struct cgroupfs_root *root;
  165. struct list_head sibling;
  166. void *private;
  167. };
  168. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  169. #include <linux/cgroup_subsys.h>
  170. #undef SUBSYS
  171. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  172. struct cgroup *cont, int subsys_id)
  173. {
  174. return cont->subsys[subsys_id];
  175. }
  176. static inline struct cgroup_subsys_state *task_subsys_state(
  177. struct task_struct *task, int subsys_id)
  178. {
  179. return rcu_dereference(task->cgroups.subsys[subsys_id]);
  180. }
  181. static inline struct cgroup* task_cgroup(struct task_struct *task,
  182. int subsys_id)
  183. {
  184. return task_subsys_state(task, subsys_id)->cgroup;
  185. }
  186. int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
  187. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
  188. #else /* !CONFIG_CGROUPS */
  189. static inline int cgroup_init_early(void) { return 0; }
  190. static inline int cgroup_init(void) { return 0; }
  191. static inline void cgroup_init_smp(void) {}
  192. static inline void cgroup_fork(struct task_struct *p) {}
  193. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  194. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  195. static inline void cgroup_lock(void) {}
  196. static inline void cgroup_unlock(void) {}
  197. #endif /* !CONFIG_CGROUPS */
  198. #endif /* _LINUX_CGROUP_H */