cgroup.h 6.0 KB

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