1 #ifndef _LINUX_CGROUP_H
2 #define _LINUX_CGROUP_H
6 * Copyright (C) 2003 BULL SA
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 #include <linux/sched.h>
12 #include <linux/cpumask.h>
13 #include <linux/nodemask.h>
14 #include <linux/rcupdate.h>
15 #include <linux/cgroupstats.h>
16 #include <linux/prio_heap.h>
17 #include <linux/rwsem.h>
26 extern int cgroup_init_early(void);
27 extern int cgroup_init(void);
28 extern void cgroup_lock(void);
29 extern bool cgroup_lock_live_group(struct cgroup
*cgrp
);
30 extern void cgroup_unlock(void);
31 extern void cgroup_fork(struct task_struct
*p
);
32 extern void cgroup_fork_callbacks(struct task_struct
*p
);
33 extern void cgroup_post_fork(struct task_struct
*p
);
34 extern void cgroup_exit(struct task_struct
*p
, int run_callbacks
);
35 extern int cgroupstats_build(struct cgroupstats
*stats
,
36 struct dentry
*dentry
);
38 extern struct file_operations proc_cgroup_operations
;
40 /* Define the enumeration of all cgroup subsystems */
41 #define SUBSYS(_x) _x ## _subsys_id,
42 enum cgroup_subsys_id
{
43 #include <linux/cgroup_subsys.h>
48 /* Per-subsystem/per-cgroup state maintained by the system. */
49 struct cgroup_subsys_state
{
51 * The cgroup that this subsystem is attached to. Useful
52 * for subsystems that want to know about the cgroup
55 struct cgroup
*cgroup
;
58 * State maintained by the cgroup system to allow subsystems
59 * to be "busy". Should be accessed via css_get(),
60 * css_tryget() and and css_put().
68 /* bits in struct cgroup_subsys_state flags field */
70 CSS_ROOT
, /* This CSS is the root of the subsystem */
71 CSS_REMOVED
, /* This CSS is dead */
75 * Call css_get() to hold a reference on the css; it can be used
76 * for a reference obtained via:
77 * - an existing ref-counted reference to the css
78 * - task->cgroups for a locked task
81 static inline void css_get(struct cgroup_subsys_state
*css
)
83 /* We don't need to reference count the root state */
84 if (!test_bit(CSS_ROOT
, &css
->flags
))
85 atomic_inc(&css
->refcnt
);
88 static inline bool css_is_removed(struct cgroup_subsys_state
*css
)
90 return test_bit(CSS_REMOVED
, &css
->flags
);
94 * Call css_tryget() to take a reference on a css if your existing
95 * (known-valid) reference isn't already ref-counted. Returns false if
96 * the css has been destroyed.
99 static inline bool css_tryget(struct cgroup_subsys_state
*css
)
101 if (test_bit(CSS_ROOT
, &css
->flags
))
103 while (!atomic_inc_not_zero(&css
->refcnt
)) {
104 if (test_bit(CSS_REMOVED
, &css
->flags
))
112 * css_put() should be called to release a reference taken by
113 * css_get() or css_tryget()
116 extern void __css_put(struct cgroup_subsys_state
*css
);
117 static inline void css_put(struct cgroup_subsys_state
*css
)
119 if (!test_bit(CSS_ROOT
, &css
->flags
))
123 /* bits in struct cgroup flags field */
125 /* Control Group is dead */
128 * Control Group has previously had a child cgroup or a task,
129 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
132 /* Control Group requires release notifications to userspace */
133 CGRP_NOTIFY_ON_RELEASE
,
137 unsigned long flags
; /* "unsigned long" so bitops work */
140 * count users of this cgroup. >0 means busy, but doesn't
141 * necessarily indicate the number of tasks in the cgroup
146 * We link our 'sibling' struct into our parent's 'children'.
147 * Our children link their 'sibling' into our 'children'.
149 struct list_head sibling
; /* my parent's children */
150 struct list_head children
; /* my children */
152 struct cgroup
*parent
; /* my parent */
153 struct dentry
*dentry
; /* cgroup fs entry, RCU protected */
155 /* Private pointers for each registered subsystem */
156 struct cgroup_subsys_state
*subsys
[CGROUP_SUBSYS_COUNT
];
158 struct cgroupfs_root
*root
;
159 struct cgroup
*top_cgroup
;
162 * List of cg_cgroup_links pointing at css_sets with
163 * tasks in this cgroup. Protected by css_set_lock
165 struct list_head css_sets
;
168 * Linked list running through all cgroups that can
169 * potentially be reaped by the release agent. Protected by
172 struct list_head release_list
;
174 /* pids_mutex protects the fields below */
175 struct rw_semaphore pids_mutex
;
176 /* Array of process ids in the cgroup */
178 /* How many files are using the current tasks_pids array */
180 /* Length of the current tasks_pids array */
183 /* For RCU-protected deletion */
184 struct rcu_head rcu_head
;
188 * A css_set is a structure holding pointers to a set of
189 * cgroup_subsys_state objects. This saves space in the task struct
190 * object and speeds up fork()/exit(), since a single inc/dec and a
191 * list_add()/del() can bump the reference count on the entire cgroup
197 /* Reference count */
201 * List running through all cgroup groups in the same hash
202 * slot. Protected by css_set_lock
204 struct hlist_node hlist
;
207 * List running through all tasks using this cgroup
208 * group. Protected by css_set_lock
210 struct list_head tasks
;
213 * List of cg_cgroup_link objects on link chains from
214 * cgroups referenced from this css_set. Protected by
217 struct list_head cg_links
;
220 * Set of subsystem states, one for each subsystem. This array
221 * is immutable after creation apart from the init_css_set
222 * during subsystem registration (at boot time).
224 struct cgroup_subsys_state
*subsys
[CGROUP_SUBSYS_COUNT
];
228 * cgroup_map_cb is an abstract callback API for reporting map-valued
232 struct cgroup_map_cb
{
233 int (*fill
)(struct cgroup_map_cb
*cb
, const char *key
, u64 value
);
238 * struct cftype: handler definitions for cgroup control files
240 * When reading/writing to a file:
241 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
242 * - the 'cftype' of the file is file->f_dentry->d_fsdata
245 #define MAX_CFTYPE_NAME 64
248 * By convention, the name should begin with the name of the
249 * subsystem, followed by a period
251 char name
[MAX_CFTYPE_NAME
];
255 * If non-zero, defines the maximum length of string that can
256 * be passed to write_string; defaults to 64
258 size_t max_write_len
;
260 int (*open
)(struct inode
*inode
, struct file
*file
);
261 ssize_t (*read
)(struct cgroup
*cgrp
, struct cftype
*cft
,
263 char __user
*buf
, size_t nbytes
, loff_t
*ppos
);
265 * read_u64() is a shortcut for the common case of returning a
266 * single integer. Use it in place of read()
268 u64 (*read_u64
)(struct cgroup
*cgrp
, struct cftype
*cft
);
270 * read_s64() is a signed version of read_u64()
272 s64 (*read_s64
)(struct cgroup
*cgrp
, struct cftype
*cft
);
274 * read_map() is used for defining a map of key/value
275 * pairs. It should call cb->fill(cb, key, value) for each
276 * entry. The key/value pairs (and their ordering) should not
277 * change between reboots.
279 int (*read_map
)(struct cgroup
*cont
, struct cftype
*cft
,
280 struct cgroup_map_cb
*cb
);
282 * read_seq_string() is used for outputting a simple sequence
285 int (*read_seq_string
)(struct cgroup
*cont
, struct cftype
*cft
,
288 ssize_t (*write
)(struct cgroup
*cgrp
, struct cftype
*cft
,
290 const char __user
*buf
, size_t nbytes
, loff_t
*ppos
);
293 * write_u64() is a shortcut for the common case of accepting
294 * a single integer (as parsed by simple_strtoull) from
295 * userspace. Use in place of write(); return 0 or error.
297 int (*write_u64
)(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
);
299 * write_s64() is a signed version of write_u64()
301 int (*write_s64
)(struct cgroup
*cgrp
, struct cftype
*cft
, s64 val
);
304 * write_string() is passed a nul-terminated kernelspace
305 * buffer of maximum length determined by max_write_len.
306 * Returns 0 or -ve error code.
308 int (*write_string
)(struct cgroup
*cgrp
, struct cftype
*cft
,
311 * trigger() callback can be used to get some kick from the
312 * userspace, when the actual string written is not important
313 * at all. The private field can be used to determine the
314 * kick type for multiplexing.
316 int (*trigger
)(struct cgroup
*cgrp
, unsigned int event
);
318 int (*release
)(struct inode
*inode
, struct file
*file
);
321 struct cgroup_scanner
{
323 int (*test_task
)(struct task_struct
*p
, struct cgroup_scanner
*scan
);
324 void (*process_task
)(struct task_struct
*p
,
325 struct cgroup_scanner
*scan
);
326 struct ptr_heap
*heap
;
330 * Add a new file to the given cgroup directory. Should only be
331 * called by subsystems from within a populate() method
333 int cgroup_add_file(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
334 const struct cftype
*cft
);
337 * Add a set of new files to the given cgroup directory. Should
338 * only be called by subsystems from within a populate() method
340 int cgroup_add_files(struct cgroup
*cgrp
,
341 struct cgroup_subsys
*subsys
,
342 const struct cftype cft
[],
345 int cgroup_is_removed(const struct cgroup
*cgrp
);
347 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
);
349 int cgroup_task_count(const struct cgroup
*cgrp
);
351 /* Return true if cgrp is a descendant of the task's cgroup */
352 int cgroup_is_descendant(const struct cgroup
*cgrp
, struct task_struct
*task
);
354 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
356 struct cgroup_subsys
{
357 struct cgroup_subsys_state
*(*create
)(struct cgroup_subsys
*ss
,
358 struct cgroup
*cgrp
);
359 void (*pre_destroy
)(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
);
360 void (*destroy
)(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
);
361 int (*can_attach
)(struct cgroup_subsys
*ss
,
362 struct cgroup
*cgrp
, struct task_struct
*tsk
);
363 void (*attach
)(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
,
364 struct cgroup
*old_cgrp
, struct task_struct
*tsk
);
365 void (*fork
)(struct cgroup_subsys
*ss
, struct task_struct
*task
);
366 void (*exit
)(struct cgroup_subsys
*ss
, struct task_struct
*task
);
367 int (*populate
)(struct cgroup_subsys
*ss
,
368 struct cgroup
*cgrp
);
369 void (*post_clone
)(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
);
370 void (*bind
)(struct cgroup_subsys
*ss
, struct cgroup
*root
);
376 #define MAX_CGROUP_TYPE_NAMELEN 32
380 * Protects sibling/children links of cgroups in this
381 * hierarchy, plus protects which hierarchy (or none) the
382 * subsystem is a part of (i.e. root/sibling). To avoid
383 * potential deadlocks, the following operations should not be
384 * undertaken while holding any hierarchy_mutex:
386 * - allocating memory
387 * - initiating hotplug events
389 struct mutex hierarchy_mutex
;
390 struct lock_class_key subsys_key
;
393 * Link to parent, and list entry in parent's children.
394 * Protected by this->hierarchy_mutex and cgroup_lock()
396 struct cgroupfs_root
*root
;
397 struct list_head sibling
;
400 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
401 #include <linux/cgroup_subsys.h>
404 static inline struct cgroup_subsys_state
*cgroup_subsys_state(
405 struct cgroup
*cgrp
, int subsys_id
)
407 return cgrp
->subsys
[subsys_id
];
410 static inline struct cgroup_subsys_state
*task_subsys_state(
411 struct task_struct
*task
, int subsys_id
)
413 return rcu_dereference(task
->cgroups
->subsys
[subsys_id
]);
416 static inline struct cgroup
* task_cgroup(struct task_struct
*task
,
419 return task_subsys_state(task
, subsys_id
)->cgroup
;
422 int cgroup_clone(struct task_struct
*tsk
, struct cgroup_subsys
*ss
,
425 /* A cgroup_iter should be treated as an opaque object */
427 struct list_head
*cg_link
;
428 struct list_head
*task
;
432 * To iterate across the tasks in a cgroup:
434 * 1) call cgroup_iter_start to intialize an iterator
436 * 2) call cgroup_iter_next() to retrieve member tasks until it
437 * returns NULL or until you want to end the iteration
439 * 3) call cgroup_iter_end() to destroy the iterator.
441 * Or, call cgroup_scan_tasks() to iterate through every task in a
442 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
443 * the test_task() callback, but not while calling the process_task()
446 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
);
447 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
448 struct cgroup_iter
*it
);
449 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
);
450 int cgroup_scan_tasks(struct cgroup_scanner
*scan
);
451 int cgroup_attach_task(struct cgroup
*, struct task_struct
*);
453 #else /* !CONFIG_CGROUPS */
455 static inline int cgroup_init_early(void) { return 0; }
456 static inline int cgroup_init(void) { return 0; }
457 static inline void cgroup_fork(struct task_struct
*p
) {}
458 static inline void cgroup_fork_callbacks(struct task_struct
*p
) {}
459 static inline void cgroup_post_fork(struct task_struct
*p
) {}
460 static inline void cgroup_exit(struct task_struct
*p
, int callbacks
) {}
462 static inline void cgroup_lock(void) {}
463 static inline void cgroup_unlock(void) {}
464 static inline int cgroupstats_build(struct cgroupstats
*stats
,
465 struct dentry
*dentry
)
470 #endif /* !CONFIG_CGROUPS */
472 #endif /* _LINUX_CGROUP_H */