cgroup.h 6.8 KB

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