cgroups: use a hash table for css_set finding
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / cgroup.c
1 /*
2 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25 #include <linux/cgroup.h>
26 #include <linux/errno.h>
27 #include <linux/fs.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/mm.h>
31 #include <linux/mutex.h>
32 #include <linux/mount.h>
33 #include <linux/pagemap.h>
34 #include <linux/proc_fs.h>
35 #include <linux/rcupdate.h>
36 #include <linux/sched.h>
37 #include <linux/backing-dev.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/magic.h>
41 #include <linux/spinlock.h>
42 #include <linux/string.h>
43 #include <linux/sort.h>
44 #include <linux/kmod.h>
45 #include <linux/delayacct.h>
46 #include <linux/cgroupstats.h>
47 #include <linux/hash.h>
48
49 #include <asm/atomic.h>
50
51 static DEFINE_MUTEX(cgroup_mutex);
52
53 /* Generate an array of cgroup subsystem pointers */
54 #define SUBSYS(_x) &_x ## _subsys,
55
56 static struct cgroup_subsys *subsys[] = {
57 #include <linux/cgroup_subsys.h>
58 };
59
60 /*
61 * A cgroupfs_root represents the root of a cgroup hierarchy,
62 * and may be associated with a superblock to form an active
63 * hierarchy
64 */
65 struct cgroupfs_root {
66 struct super_block *sb;
67
68 /*
69 * The bitmask of subsystems intended to be attached to this
70 * hierarchy
71 */
72 unsigned long subsys_bits;
73
74 /* The bitmask of subsystems currently attached to this hierarchy */
75 unsigned long actual_subsys_bits;
76
77 /* A list running through the attached subsystems */
78 struct list_head subsys_list;
79
80 /* The root cgroup for this hierarchy */
81 struct cgroup top_cgroup;
82
83 /* Tracks how many cgroups are currently defined in hierarchy.*/
84 int number_of_cgroups;
85
86 /* A list running through the mounted hierarchies */
87 struct list_head root_list;
88
89 /* Hierarchy-specific flags */
90 unsigned long flags;
91
92 /* The path to use for release notifications. No locking
93 * between setting and use - so if userspace updates this
94 * while child cgroups exist, you could miss a
95 * notification. We ensure that it's always a valid
96 * NUL-terminated string */
97 char release_agent_path[PATH_MAX];
98 };
99
100
101 /*
102 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
103 * subsystems that are otherwise unattached - it never has more than a
104 * single cgroup, and all tasks are part of that cgroup.
105 */
106 static struct cgroupfs_root rootnode;
107
108 /* The list of hierarchy roots */
109
110 static LIST_HEAD(roots);
111 static int root_count;
112
113 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
114 #define dummytop (&rootnode.top_cgroup)
115
116 /* This flag indicates whether tasks in the fork and exit paths should
117 * check for fork/exit handlers to call. This avoids us having to do
118 * extra work in the fork/exit path if none of the subsystems need to
119 * be called.
120 */
121 static int need_forkexit_callback;
122
123 /* convenient tests for these bits */
124 inline int cgroup_is_removed(const struct cgroup *cgrp)
125 {
126 return test_bit(CGRP_REMOVED, &cgrp->flags);
127 }
128
129 /* bits in struct cgroupfs_root flags field */
130 enum {
131 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
132 };
133
134 static int cgroup_is_releasable(const struct cgroup *cgrp)
135 {
136 const int bits =
137 (1 << CGRP_RELEASABLE) |
138 (1 << CGRP_NOTIFY_ON_RELEASE);
139 return (cgrp->flags & bits) == bits;
140 }
141
142 static int notify_on_release(const struct cgroup *cgrp)
143 {
144 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
145 }
146
147 /*
148 * for_each_subsys() allows you to iterate on each subsystem attached to
149 * an active hierarchy
150 */
151 #define for_each_subsys(_root, _ss) \
152 list_for_each_entry(_ss, &_root->subsys_list, sibling)
153
154 /* for_each_root() allows you to iterate across the active hierarchies */
155 #define for_each_root(_root) \
156 list_for_each_entry(_root, &roots, root_list)
157
158 /* the list of cgroups eligible for automatic release. Protected by
159 * release_list_lock */
160 static LIST_HEAD(release_list);
161 static DEFINE_SPINLOCK(release_list_lock);
162 static void cgroup_release_agent(struct work_struct *work);
163 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
164 static void check_for_release(struct cgroup *cgrp);
165
166 /* Link structure for associating css_set objects with cgroups */
167 struct cg_cgroup_link {
168 /*
169 * List running through cg_cgroup_links associated with a
170 * cgroup, anchored on cgroup->css_sets
171 */
172 struct list_head cgrp_link_list;
173 /*
174 * List running through cg_cgroup_links pointing at a
175 * single css_set object, anchored on css_set->cg_links
176 */
177 struct list_head cg_link_list;
178 struct css_set *cg;
179 };
180
181 /* The default css_set - used by init and its children prior to any
182 * hierarchies being mounted. It contains a pointer to the root state
183 * for each subsystem. Also used to anchor the list of css_sets. Not
184 * reference-counted, to improve performance when child cgroups
185 * haven't been created.
186 */
187
188 static struct css_set init_css_set;
189 static struct cg_cgroup_link init_css_set_link;
190
191 /* css_set_lock protects the list of css_set objects, and the
192 * chain of tasks off each css_set. Nests outside task->alloc_lock
193 * due to cgroup_iter_start() */
194 static DEFINE_RWLOCK(css_set_lock);
195 static int css_set_count;
196
197 /* hash table for cgroup groups. This improves the performance to
198 * find an existing css_set */
199 #define CSS_SET_HASH_BITS 7
200 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
201 static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
202
203 static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
204 {
205 int i;
206 int index;
207 unsigned long tmp = 0UL;
208
209 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
210 tmp += (unsigned long)css[i];
211 tmp = (tmp >> 16) ^ tmp;
212
213 index = hash_long(tmp, CSS_SET_HASH_BITS);
214
215 return &css_set_table[index];
216 }
217
218 /* We don't maintain the lists running through each css_set to its
219 * task until after the first call to cgroup_iter_start(). This
220 * reduces the fork()/exit() overhead for people who have cgroups
221 * compiled into their kernel but not actually in use */
222 static int use_task_css_set_links;
223
224 /* When we create or destroy a css_set, the operation simply
225 * takes/releases a reference count on all the cgroups referenced
226 * by subsystems in this css_set. This can end up multiple-counting
227 * some cgroups, but that's OK - the ref-count is just a
228 * busy/not-busy indicator; ensuring that we only count each cgroup
229 * once would require taking a global lock to ensure that no
230 * subsystems moved between hierarchies while we were doing so.
231 *
232 * Possible TODO: decide at boot time based on the number of
233 * registered subsystems and the number of CPUs or NUMA nodes whether
234 * it's better for performance to ref-count every subsystem, or to
235 * take a global lock and only add one ref count to each hierarchy.
236 */
237
238 /*
239 * unlink a css_set from the list and free it
240 */
241 static void unlink_css_set(struct css_set *cg)
242 {
243 write_lock(&css_set_lock);
244 hlist_del(&cg->hlist);
245 list_del(&cg->list);
246 css_set_count--;
247 while (!list_empty(&cg->cg_links)) {
248 struct cg_cgroup_link *link;
249 link = list_entry(cg->cg_links.next,
250 struct cg_cgroup_link, cg_link_list);
251 list_del(&link->cg_link_list);
252 list_del(&link->cgrp_link_list);
253 kfree(link);
254 }
255 write_unlock(&css_set_lock);
256 }
257
258 static void __release_css_set(struct kref *k, int taskexit)
259 {
260 int i;
261 struct css_set *cg = container_of(k, struct css_set, ref);
262
263 unlink_css_set(cg);
264
265 rcu_read_lock();
266 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
267 struct cgroup *cgrp = cg->subsys[i]->cgroup;
268 if (atomic_dec_and_test(&cgrp->count) &&
269 notify_on_release(cgrp)) {
270 if (taskexit)
271 set_bit(CGRP_RELEASABLE, &cgrp->flags);
272 check_for_release(cgrp);
273 }
274 }
275 rcu_read_unlock();
276 kfree(cg);
277 }
278
279 static void release_css_set(struct kref *k)
280 {
281 __release_css_set(k, 0);
282 }
283
284 static void release_css_set_taskexit(struct kref *k)
285 {
286 __release_css_set(k, 1);
287 }
288
289 /*
290 * refcounted get/put for css_set objects
291 */
292 static inline void get_css_set(struct css_set *cg)
293 {
294 kref_get(&cg->ref);
295 }
296
297 static inline void put_css_set(struct css_set *cg)
298 {
299 kref_put(&cg->ref, release_css_set);
300 }
301
302 static inline void put_css_set_taskexit(struct css_set *cg)
303 {
304 kref_put(&cg->ref, release_css_set_taskexit);
305 }
306
307 /*
308 * find_existing_css_set() is a helper for
309 * find_css_set(), and checks to see whether an existing
310 * css_set is suitable.
311 *
312 * oldcg: the cgroup group that we're using before the cgroup
313 * transition
314 *
315 * cgrp: the cgroup that we're moving into
316 *
317 * template: location in which to build the desired set of subsystem
318 * state objects for the new cgroup group
319 */
320 static struct css_set *find_existing_css_set(
321 struct css_set *oldcg,
322 struct cgroup *cgrp,
323 struct cgroup_subsys_state *template[])
324 {
325 int i;
326 struct cgroupfs_root *root = cgrp->root;
327 struct hlist_head *hhead;
328 struct hlist_node *node;
329 struct css_set *cg;
330
331 /* Built the set of subsystem state objects that we want to
332 * see in the new css_set */
333 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
334 if (root->subsys_bits & (1UL << i)) {
335 /* Subsystem is in this hierarchy. So we want
336 * the subsystem state from the new
337 * cgroup */
338 template[i] = cgrp->subsys[i];
339 } else {
340 /* Subsystem is not in this hierarchy, so we
341 * don't want to change the subsystem state */
342 template[i] = oldcg->subsys[i];
343 }
344 }
345
346 hhead = css_set_hash(template);
347 hlist_for_each_entry(cg, node, hhead, hlist) {
348 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
349 /* All subsystems matched */
350 return cg;
351 }
352 }
353
354 /* No existing cgroup group matched */
355 return NULL;
356 }
357
358 /*
359 * allocate_cg_links() allocates "count" cg_cgroup_link structures
360 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
361 * success or a negative error
362 */
363 static int allocate_cg_links(int count, struct list_head *tmp)
364 {
365 struct cg_cgroup_link *link;
366 int i;
367 INIT_LIST_HEAD(tmp);
368 for (i = 0; i < count; i++) {
369 link = kmalloc(sizeof(*link), GFP_KERNEL);
370 if (!link) {
371 while (!list_empty(tmp)) {
372 link = list_entry(tmp->next,
373 struct cg_cgroup_link,
374 cgrp_link_list);
375 list_del(&link->cgrp_link_list);
376 kfree(link);
377 }
378 return -ENOMEM;
379 }
380 list_add(&link->cgrp_link_list, tmp);
381 }
382 return 0;
383 }
384
385 static void free_cg_links(struct list_head *tmp)
386 {
387 while (!list_empty(tmp)) {
388 struct cg_cgroup_link *link;
389 link = list_entry(tmp->next,
390 struct cg_cgroup_link,
391 cgrp_link_list);
392 list_del(&link->cgrp_link_list);
393 kfree(link);
394 }
395 }
396
397 /*
398 * find_css_set() takes an existing cgroup group and a
399 * cgroup object, and returns a css_set object that's
400 * equivalent to the old group, but with the given cgroup
401 * substituted into the appropriate hierarchy. Must be called with
402 * cgroup_mutex held
403 */
404 static struct css_set *find_css_set(
405 struct css_set *oldcg, struct cgroup *cgrp)
406 {
407 struct css_set *res;
408 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
409 int i;
410
411 struct list_head tmp_cg_links;
412 struct cg_cgroup_link *link;
413
414 struct hlist_head *hhead;
415
416 /* First see if we already have a cgroup group that matches
417 * the desired set */
418 write_lock(&css_set_lock);
419 res = find_existing_css_set(oldcg, cgrp, template);
420 if (res)
421 get_css_set(res);
422 write_unlock(&css_set_lock);
423
424 if (res)
425 return res;
426
427 res = kmalloc(sizeof(*res), GFP_KERNEL);
428 if (!res)
429 return NULL;
430
431 /* Allocate all the cg_cgroup_link objects that we'll need */
432 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
433 kfree(res);
434 return NULL;
435 }
436
437 kref_init(&res->ref);
438 INIT_LIST_HEAD(&res->cg_links);
439 INIT_LIST_HEAD(&res->tasks);
440 INIT_HLIST_NODE(&res->hlist);
441
442 /* Copy the set of subsystem state objects generated in
443 * find_existing_css_set() */
444 memcpy(res->subsys, template, sizeof(res->subsys));
445
446 write_lock(&css_set_lock);
447 /* Add reference counts and links from the new css_set. */
448 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
449 struct cgroup *cgrp = res->subsys[i]->cgroup;
450 struct cgroup_subsys *ss = subsys[i];
451 atomic_inc(&cgrp->count);
452 /*
453 * We want to add a link once per cgroup, so we
454 * only do it for the first subsystem in each
455 * hierarchy
456 */
457 if (ss->root->subsys_list.next == &ss->sibling) {
458 BUG_ON(list_empty(&tmp_cg_links));
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
461 cgrp_link_list);
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &cgrp->css_sets);
464 link->cg = res;
465 list_add(&link->cg_link_list, &res->cg_links);
466 }
467 }
468 if (list_empty(&rootnode.subsys_list)) {
469 link = list_entry(tmp_cg_links.next,
470 struct cg_cgroup_link,
471 cgrp_link_list);
472 list_del(&link->cgrp_link_list);
473 list_add(&link->cgrp_link_list, &dummytop->css_sets);
474 link->cg = res;
475 list_add(&link->cg_link_list, &res->cg_links);
476 }
477
478 BUG_ON(!list_empty(&tmp_cg_links));
479
480 /* Link this cgroup group into the list */
481 list_add(&res->list, &init_css_set.list);
482 css_set_count++;
483
484 /* Add this cgroup group to the hash table */
485 hhead = css_set_hash(res->subsys);
486 hlist_add_head(&res->hlist, hhead);
487
488 write_unlock(&css_set_lock);
489
490 return res;
491 }
492
493 /*
494 * There is one global cgroup mutex. We also require taking
495 * task_lock() when dereferencing a task's cgroup subsys pointers.
496 * See "The task_lock() exception", at the end of this comment.
497 *
498 * A task must hold cgroup_mutex to modify cgroups.
499 *
500 * Any task can increment and decrement the count field without lock.
501 * So in general, code holding cgroup_mutex can't rely on the count
502 * field not changing. However, if the count goes to zero, then only
503 * cgroup_attach_task() can increment it again. Because a count of zero
504 * means that no tasks are currently attached, therefore there is no
505 * way a task attached to that cgroup can fork (the other way to
506 * increment the count). So code holding cgroup_mutex can safely
507 * assume that if the count is zero, it will stay zero. Similarly, if
508 * a task holds cgroup_mutex on a cgroup with zero count, it
509 * knows that the cgroup won't be removed, as cgroup_rmdir()
510 * needs that mutex.
511 *
512 * The cgroup_common_file_write handler for operations that modify
513 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
514 * single threading all such cgroup modifications across the system.
515 *
516 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
517 * (usually) take cgroup_mutex. These are the two most performance
518 * critical pieces of code here. The exception occurs on cgroup_exit(),
519 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
520 * is taken, and if the cgroup count is zero, a usermode call made
521 * to the release agent with the name of the cgroup (path relative to
522 * the root of cgroup file system) as the argument.
523 *
524 * A cgroup can only be deleted if both its 'count' of using tasks
525 * is zero, and its list of 'children' cgroups is empty. Since all
526 * tasks in the system use _some_ cgroup, and since there is always at
527 * least one task in the system (init, pid == 1), therefore, top_cgroup
528 * always has either children cgroups and/or using tasks. So we don't
529 * need a special hack to ensure that top_cgroup cannot be deleted.
530 *
531 * The task_lock() exception
532 *
533 * The need for this exception arises from the action of
534 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
535 * another. It does so using cgroup_mutex, however there are
536 * several performance critical places that need to reference
537 * task->cgroup without the expense of grabbing a system global
538 * mutex. Therefore except as noted below, when dereferencing or, as
539 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
540 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
541 * the task_struct routinely used for such matters.
542 *
543 * P.S. One more locking exception. RCU is used to guard the
544 * update of a tasks cgroup pointer by cgroup_attach_task()
545 */
546
547 /**
548 * cgroup_lock - lock out any changes to cgroup structures
549 *
550 */
551 void cgroup_lock(void)
552 {
553 mutex_lock(&cgroup_mutex);
554 }
555
556 /**
557 * cgroup_unlock - release lock on cgroup changes
558 *
559 * Undo the lock taken in a previous cgroup_lock() call.
560 */
561 void cgroup_unlock(void)
562 {
563 mutex_unlock(&cgroup_mutex);
564 }
565
566 /*
567 * A couple of forward declarations required, due to cyclic reference loop:
568 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
569 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
570 * -> cgroup_mkdir.
571 */
572
573 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
574 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
575 static int cgroup_populate_dir(struct cgroup *cgrp);
576 static struct inode_operations cgroup_dir_inode_operations;
577 static struct file_operations proc_cgroupstats_operations;
578
579 static struct backing_dev_info cgroup_backing_dev_info = {
580 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
581 };
582
583 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
584 {
585 struct inode *inode = new_inode(sb);
586
587 if (inode) {
588 inode->i_mode = mode;
589 inode->i_uid = current->fsuid;
590 inode->i_gid = current->fsgid;
591 inode->i_blocks = 0;
592 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
593 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
594 }
595 return inode;
596 }
597
598 /*
599 * Call subsys's pre_destroy handler.
600 * This is called before css refcnt check.
601 */
602 static void cgroup_call_pre_destroy(struct cgroup *cgrp)
603 {
604 struct cgroup_subsys *ss;
605 for_each_subsys(cgrp->root, ss)
606 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
607 ss->pre_destroy(ss, cgrp);
608 return;
609 }
610
611 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
612 {
613 /* is dentry a directory ? if so, kfree() associated cgroup */
614 if (S_ISDIR(inode->i_mode)) {
615 struct cgroup *cgrp = dentry->d_fsdata;
616 struct cgroup_subsys *ss;
617 BUG_ON(!(cgroup_is_removed(cgrp)));
618 /* It's possible for external users to be holding css
619 * reference counts on a cgroup; css_put() needs to
620 * be able to access the cgroup after decrementing
621 * the reference count in order to know if it needs to
622 * queue the cgroup to be handled by the release
623 * agent */
624 synchronize_rcu();
625
626 mutex_lock(&cgroup_mutex);
627 /*
628 * Release the subsystem state objects.
629 */
630 for_each_subsys(cgrp->root, ss) {
631 if (cgrp->subsys[ss->subsys_id])
632 ss->destroy(ss, cgrp);
633 }
634
635 cgrp->root->number_of_cgroups--;
636 mutex_unlock(&cgroup_mutex);
637
638 /* Drop the active superblock reference that we took when we
639 * created the cgroup */
640 deactivate_super(cgrp->root->sb);
641
642 kfree(cgrp);
643 }
644 iput(inode);
645 }
646
647 static void remove_dir(struct dentry *d)
648 {
649 struct dentry *parent = dget(d->d_parent);
650
651 d_delete(d);
652 simple_rmdir(parent->d_inode, d);
653 dput(parent);
654 }
655
656 static void cgroup_clear_directory(struct dentry *dentry)
657 {
658 struct list_head *node;
659
660 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
661 spin_lock(&dcache_lock);
662 node = dentry->d_subdirs.next;
663 while (node != &dentry->d_subdirs) {
664 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
665 list_del_init(node);
666 if (d->d_inode) {
667 /* This should never be called on a cgroup
668 * directory with child cgroups */
669 BUG_ON(d->d_inode->i_mode & S_IFDIR);
670 d = dget_locked(d);
671 spin_unlock(&dcache_lock);
672 d_delete(d);
673 simple_unlink(dentry->d_inode, d);
674 dput(d);
675 spin_lock(&dcache_lock);
676 }
677 node = dentry->d_subdirs.next;
678 }
679 spin_unlock(&dcache_lock);
680 }
681
682 /*
683 * NOTE : the dentry must have been dget()'ed
684 */
685 static void cgroup_d_remove_dir(struct dentry *dentry)
686 {
687 cgroup_clear_directory(dentry);
688
689 spin_lock(&dcache_lock);
690 list_del_init(&dentry->d_u.d_child);
691 spin_unlock(&dcache_lock);
692 remove_dir(dentry);
693 }
694
695 static int rebind_subsystems(struct cgroupfs_root *root,
696 unsigned long final_bits)
697 {
698 unsigned long added_bits, removed_bits;
699 struct cgroup *cgrp = &root->top_cgroup;
700 int i;
701
702 removed_bits = root->actual_subsys_bits & ~final_bits;
703 added_bits = final_bits & ~root->actual_subsys_bits;
704 /* Check that any added subsystems are currently free */
705 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
706 unsigned long bit = 1UL << i;
707 struct cgroup_subsys *ss = subsys[i];
708 if (!(bit & added_bits))
709 continue;
710 if (ss->root != &rootnode) {
711 /* Subsystem isn't free */
712 return -EBUSY;
713 }
714 }
715
716 /* Currently we don't handle adding/removing subsystems when
717 * any child cgroups exist. This is theoretically supportable
718 * but involves complex error handling, so it's being left until
719 * later */
720 if (!list_empty(&cgrp->children))
721 return -EBUSY;
722
723 /* Process each subsystem */
724 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
725 struct cgroup_subsys *ss = subsys[i];
726 unsigned long bit = 1UL << i;
727 if (bit & added_bits) {
728 /* We're binding this subsystem to this hierarchy */
729 BUG_ON(cgrp->subsys[i]);
730 BUG_ON(!dummytop->subsys[i]);
731 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
732 cgrp->subsys[i] = dummytop->subsys[i];
733 cgrp->subsys[i]->cgroup = cgrp;
734 list_add(&ss->sibling, &root->subsys_list);
735 rcu_assign_pointer(ss->root, root);
736 if (ss->bind)
737 ss->bind(ss, cgrp);
738
739 } else if (bit & removed_bits) {
740 /* We're removing this subsystem */
741 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
742 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
743 if (ss->bind)
744 ss->bind(ss, dummytop);
745 dummytop->subsys[i]->cgroup = dummytop;
746 cgrp->subsys[i] = NULL;
747 rcu_assign_pointer(subsys[i]->root, &rootnode);
748 list_del(&ss->sibling);
749 } else if (bit & final_bits) {
750 /* Subsystem state should already exist */
751 BUG_ON(!cgrp->subsys[i]);
752 } else {
753 /* Subsystem state shouldn't exist */
754 BUG_ON(cgrp->subsys[i]);
755 }
756 }
757 root->subsys_bits = root->actual_subsys_bits = final_bits;
758 synchronize_rcu();
759
760 return 0;
761 }
762
763 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
764 {
765 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
766 struct cgroup_subsys *ss;
767
768 mutex_lock(&cgroup_mutex);
769 for_each_subsys(root, ss)
770 seq_printf(seq, ",%s", ss->name);
771 if (test_bit(ROOT_NOPREFIX, &root->flags))
772 seq_puts(seq, ",noprefix");
773 if (strlen(root->release_agent_path))
774 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
775 mutex_unlock(&cgroup_mutex);
776 return 0;
777 }
778
779 struct cgroup_sb_opts {
780 unsigned long subsys_bits;
781 unsigned long flags;
782 char *release_agent;
783 };
784
785 /* Convert a hierarchy specifier into a bitmask of subsystems and
786 * flags. */
787 static int parse_cgroupfs_options(char *data,
788 struct cgroup_sb_opts *opts)
789 {
790 char *token, *o = data ?: "all";
791
792 opts->subsys_bits = 0;
793 opts->flags = 0;
794 opts->release_agent = NULL;
795
796 while ((token = strsep(&o, ",")) != NULL) {
797 if (!*token)
798 return -EINVAL;
799 if (!strcmp(token, "all")) {
800 /* Add all non-disabled subsystems */
801 int i;
802 opts->subsys_bits = 0;
803 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
804 struct cgroup_subsys *ss = subsys[i];
805 if (!ss->disabled)
806 opts->subsys_bits |= 1ul << i;
807 }
808 } else if (!strcmp(token, "noprefix")) {
809 set_bit(ROOT_NOPREFIX, &opts->flags);
810 } else if (!strncmp(token, "release_agent=", 14)) {
811 /* Specifying two release agents is forbidden */
812 if (opts->release_agent)
813 return -EINVAL;
814 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
815 if (!opts->release_agent)
816 return -ENOMEM;
817 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
818 opts->release_agent[PATH_MAX - 1] = 0;
819 } else {
820 struct cgroup_subsys *ss;
821 int i;
822 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
823 ss = subsys[i];
824 if (!strcmp(token, ss->name)) {
825 if (!ss->disabled)
826 set_bit(i, &opts->subsys_bits);
827 break;
828 }
829 }
830 if (i == CGROUP_SUBSYS_COUNT)
831 return -ENOENT;
832 }
833 }
834
835 /* We can't have an empty hierarchy */
836 if (!opts->subsys_bits)
837 return -EINVAL;
838
839 return 0;
840 }
841
842 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
843 {
844 int ret = 0;
845 struct cgroupfs_root *root = sb->s_fs_info;
846 struct cgroup *cgrp = &root->top_cgroup;
847 struct cgroup_sb_opts opts;
848
849 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
850 mutex_lock(&cgroup_mutex);
851
852 /* See what subsystems are wanted */
853 ret = parse_cgroupfs_options(data, &opts);
854 if (ret)
855 goto out_unlock;
856
857 /* Don't allow flags to change at remount */
858 if (opts.flags != root->flags) {
859 ret = -EINVAL;
860 goto out_unlock;
861 }
862
863 ret = rebind_subsystems(root, opts.subsys_bits);
864
865 /* (re)populate subsystem files */
866 if (!ret)
867 cgroup_populate_dir(cgrp);
868
869 if (opts.release_agent)
870 strcpy(root->release_agent_path, opts.release_agent);
871 out_unlock:
872 if (opts.release_agent)
873 kfree(opts.release_agent);
874 mutex_unlock(&cgroup_mutex);
875 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
876 return ret;
877 }
878
879 static struct super_operations cgroup_ops = {
880 .statfs = simple_statfs,
881 .drop_inode = generic_delete_inode,
882 .show_options = cgroup_show_options,
883 .remount_fs = cgroup_remount,
884 };
885
886 static void init_cgroup_root(struct cgroupfs_root *root)
887 {
888 struct cgroup *cgrp = &root->top_cgroup;
889 INIT_LIST_HEAD(&root->subsys_list);
890 INIT_LIST_HEAD(&root->root_list);
891 root->number_of_cgroups = 1;
892 cgrp->root = root;
893 cgrp->top_cgroup = cgrp;
894 INIT_LIST_HEAD(&cgrp->sibling);
895 INIT_LIST_HEAD(&cgrp->children);
896 INIT_LIST_HEAD(&cgrp->css_sets);
897 INIT_LIST_HEAD(&cgrp->release_list);
898 }
899
900 static int cgroup_test_super(struct super_block *sb, void *data)
901 {
902 struct cgroupfs_root *new = data;
903 struct cgroupfs_root *root = sb->s_fs_info;
904
905 /* First check subsystems */
906 if (new->subsys_bits != root->subsys_bits)
907 return 0;
908
909 /* Next check flags */
910 if (new->flags != root->flags)
911 return 0;
912
913 return 1;
914 }
915
916 static int cgroup_set_super(struct super_block *sb, void *data)
917 {
918 int ret;
919 struct cgroupfs_root *root = data;
920
921 ret = set_anon_super(sb, NULL);
922 if (ret)
923 return ret;
924
925 sb->s_fs_info = root;
926 root->sb = sb;
927
928 sb->s_blocksize = PAGE_CACHE_SIZE;
929 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
930 sb->s_magic = CGROUP_SUPER_MAGIC;
931 sb->s_op = &cgroup_ops;
932
933 return 0;
934 }
935
936 static int cgroup_get_rootdir(struct super_block *sb)
937 {
938 struct inode *inode =
939 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
940 struct dentry *dentry;
941
942 if (!inode)
943 return -ENOMEM;
944
945 inode->i_fop = &simple_dir_operations;
946 inode->i_op = &cgroup_dir_inode_operations;
947 /* directories start off with i_nlink == 2 (for "." entry) */
948 inc_nlink(inode);
949 dentry = d_alloc_root(inode);
950 if (!dentry) {
951 iput(inode);
952 return -ENOMEM;
953 }
954 sb->s_root = dentry;
955 return 0;
956 }
957
958 static int cgroup_get_sb(struct file_system_type *fs_type,
959 int flags, const char *unused_dev_name,
960 void *data, struct vfsmount *mnt)
961 {
962 struct cgroup_sb_opts opts;
963 int ret = 0;
964 struct super_block *sb;
965 struct cgroupfs_root *root;
966 struct list_head tmp_cg_links, *l;
967 INIT_LIST_HEAD(&tmp_cg_links);
968
969 /* First find the desired set of subsystems */
970 ret = parse_cgroupfs_options(data, &opts);
971 if (ret) {
972 if (opts.release_agent)
973 kfree(opts.release_agent);
974 return ret;
975 }
976
977 root = kzalloc(sizeof(*root), GFP_KERNEL);
978 if (!root) {
979 if (opts.release_agent)
980 kfree(opts.release_agent);
981 return -ENOMEM;
982 }
983
984 init_cgroup_root(root);
985 root->subsys_bits = opts.subsys_bits;
986 root->flags = opts.flags;
987 if (opts.release_agent) {
988 strcpy(root->release_agent_path, opts.release_agent);
989 kfree(opts.release_agent);
990 }
991
992 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
993
994 if (IS_ERR(sb)) {
995 kfree(root);
996 return PTR_ERR(sb);
997 }
998
999 if (sb->s_fs_info != root) {
1000 /* Reusing an existing superblock */
1001 BUG_ON(sb->s_root == NULL);
1002 kfree(root);
1003 root = NULL;
1004 } else {
1005 /* New superblock */
1006 struct cgroup *cgrp = &root->top_cgroup;
1007 struct inode *inode;
1008
1009 BUG_ON(sb->s_root != NULL);
1010
1011 ret = cgroup_get_rootdir(sb);
1012 if (ret)
1013 goto drop_new_super;
1014 inode = sb->s_root->d_inode;
1015
1016 mutex_lock(&inode->i_mutex);
1017 mutex_lock(&cgroup_mutex);
1018
1019 /*
1020 * We're accessing css_set_count without locking
1021 * css_set_lock here, but that's OK - it can only be
1022 * increased by someone holding cgroup_lock, and
1023 * that's us. The worst that can happen is that we
1024 * have some link structures left over
1025 */
1026 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1027 if (ret) {
1028 mutex_unlock(&cgroup_mutex);
1029 mutex_unlock(&inode->i_mutex);
1030 goto drop_new_super;
1031 }
1032
1033 ret = rebind_subsystems(root, root->subsys_bits);
1034 if (ret == -EBUSY) {
1035 mutex_unlock(&cgroup_mutex);
1036 mutex_unlock(&inode->i_mutex);
1037 goto drop_new_super;
1038 }
1039
1040 /* EBUSY should be the only error here */
1041 BUG_ON(ret);
1042
1043 list_add(&root->root_list, &roots);
1044 root_count++;
1045
1046 sb->s_root->d_fsdata = &root->top_cgroup;
1047 root->top_cgroup.dentry = sb->s_root;
1048
1049 /* Link the top cgroup in this hierarchy into all
1050 * the css_set objects */
1051 write_lock(&css_set_lock);
1052 l = &init_css_set.list;
1053 do {
1054 struct css_set *cg;
1055 struct cg_cgroup_link *link;
1056 cg = list_entry(l, struct css_set, list);
1057 BUG_ON(list_empty(&tmp_cg_links));
1058 link = list_entry(tmp_cg_links.next,
1059 struct cg_cgroup_link,
1060 cgrp_link_list);
1061 list_del(&link->cgrp_link_list);
1062 link->cg = cg;
1063 list_add(&link->cgrp_link_list,
1064 &root->top_cgroup.css_sets);
1065 list_add(&link->cg_link_list, &cg->cg_links);
1066 l = l->next;
1067 } while (l != &init_css_set.list);
1068 write_unlock(&css_set_lock);
1069
1070 free_cg_links(&tmp_cg_links);
1071
1072 BUG_ON(!list_empty(&cgrp->sibling));
1073 BUG_ON(!list_empty(&cgrp->children));
1074 BUG_ON(root->number_of_cgroups != 1);
1075
1076 cgroup_populate_dir(cgrp);
1077 mutex_unlock(&inode->i_mutex);
1078 mutex_unlock(&cgroup_mutex);
1079 }
1080
1081 return simple_set_mnt(mnt, sb);
1082
1083 drop_new_super:
1084 up_write(&sb->s_umount);
1085 deactivate_super(sb);
1086 free_cg_links(&tmp_cg_links);
1087 return ret;
1088 }
1089
1090 static void cgroup_kill_sb(struct super_block *sb) {
1091 struct cgroupfs_root *root = sb->s_fs_info;
1092 struct cgroup *cgrp = &root->top_cgroup;
1093 int ret;
1094
1095 BUG_ON(!root);
1096
1097 BUG_ON(root->number_of_cgroups != 1);
1098 BUG_ON(!list_empty(&cgrp->children));
1099 BUG_ON(!list_empty(&cgrp->sibling));
1100
1101 mutex_lock(&cgroup_mutex);
1102
1103 /* Rebind all subsystems back to the default hierarchy */
1104 ret = rebind_subsystems(root, 0);
1105 /* Shouldn't be able to fail ... */
1106 BUG_ON(ret);
1107
1108 /*
1109 * Release all the links from css_sets to this hierarchy's
1110 * root cgroup
1111 */
1112 write_lock(&css_set_lock);
1113 while (!list_empty(&cgrp->css_sets)) {
1114 struct cg_cgroup_link *link;
1115 link = list_entry(cgrp->css_sets.next,
1116 struct cg_cgroup_link, cgrp_link_list);
1117 list_del(&link->cg_link_list);
1118 list_del(&link->cgrp_link_list);
1119 kfree(link);
1120 }
1121 write_unlock(&css_set_lock);
1122
1123 if (!list_empty(&root->root_list)) {
1124 list_del(&root->root_list);
1125 root_count--;
1126 }
1127 mutex_unlock(&cgroup_mutex);
1128
1129 kfree(root);
1130 kill_litter_super(sb);
1131 }
1132
1133 static struct file_system_type cgroup_fs_type = {
1134 .name = "cgroup",
1135 .get_sb = cgroup_get_sb,
1136 .kill_sb = cgroup_kill_sb,
1137 };
1138
1139 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1140 {
1141 return dentry->d_fsdata;
1142 }
1143
1144 static inline struct cftype *__d_cft(struct dentry *dentry)
1145 {
1146 return dentry->d_fsdata;
1147 }
1148
1149 /**
1150 * cgroup_path - generate the path of a cgroup
1151 * @cgrp: the cgroup in question
1152 * @buf: the buffer to write the path into
1153 * @buflen: the length of the buffer
1154 *
1155 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1156 * Returns 0 on success, -errno on error.
1157 */
1158 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1159 {
1160 char *start;
1161
1162 if (cgrp == dummytop) {
1163 /*
1164 * Inactive subsystems have no dentry for their root
1165 * cgroup
1166 */
1167 strcpy(buf, "/");
1168 return 0;
1169 }
1170
1171 start = buf + buflen;
1172
1173 *--start = '\0';
1174 for (;;) {
1175 int len = cgrp->dentry->d_name.len;
1176 if ((start -= len) < buf)
1177 return -ENAMETOOLONG;
1178 memcpy(start, cgrp->dentry->d_name.name, len);
1179 cgrp = cgrp->parent;
1180 if (!cgrp)
1181 break;
1182 if (!cgrp->parent)
1183 continue;
1184 if (--start < buf)
1185 return -ENAMETOOLONG;
1186 *start = '/';
1187 }
1188 memmove(buf, start, buf + buflen - start);
1189 return 0;
1190 }
1191
1192 /*
1193 * Return the first subsystem attached to a cgroup's hierarchy, and
1194 * its subsystem id.
1195 */
1196
1197 static void get_first_subsys(const struct cgroup *cgrp,
1198 struct cgroup_subsys_state **css, int *subsys_id)
1199 {
1200 const struct cgroupfs_root *root = cgrp->root;
1201 const struct cgroup_subsys *test_ss;
1202 BUG_ON(list_empty(&root->subsys_list));
1203 test_ss = list_entry(root->subsys_list.next,
1204 struct cgroup_subsys, sibling);
1205 if (css) {
1206 *css = cgrp->subsys[test_ss->subsys_id];
1207 BUG_ON(!*css);
1208 }
1209 if (subsys_id)
1210 *subsys_id = test_ss->subsys_id;
1211 }
1212
1213 /**
1214 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1215 * @cgrp: the cgroup the task is attaching to
1216 * @tsk: the task to be attached
1217 *
1218 * Call holding cgroup_mutex. May take task_lock of
1219 * the task 'tsk' during call.
1220 */
1221 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1222 {
1223 int retval = 0;
1224 struct cgroup_subsys *ss;
1225 struct cgroup *oldcgrp;
1226 struct css_set *cg = tsk->cgroups;
1227 struct css_set *newcg;
1228 struct cgroupfs_root *root = cgrp->root;
1229 int subsys_id;
1230
1231 get_first_subsys(cgrp, NULL, &subsys_id);
1232
1233 /* Nothing to do if the task is already in that cgroup */
1234 oldcgrp = task_cgroup(tsk, subsys_id);
1235 if (cgrp == oldcgrp)
1236 return 0;
1237
1238 for_each_subsys(root, ss) {
1239 if (ss->can_attach) {
1240 retval = ss->can_attach(ss, cgrp, tsk);
1241 if (retval)
1242 return retval;
1243 }
1244 }
1245
1246 /*
1247 * Locate or allocate a new css_set for this task,
1248 * based on its final set of cgroups
1249 */
1250 newcg = find_css_set(cg, cgrp);
1251 if (!newcg)
1252 return -ENOMEM;
1253
1254 task_lock(tsk);
1255 if (tsk->flags & PF_EXITING) {
1256 task_unlock(tsk);
1257 put_css_set(newcg);
1258 return -ESRCH;
1259 }
1260 rcu_assign_pointer(tsk->cgroups, newcg);
1261 task_unlock(tsk);
1262
1263 /* Update the css_set linked lists if we're using them */
1264 write_lock(&css_set_lock);
1265 if (!list_empty(&tsk->cg_list)) {
1266 list_del(&tsk->cg_list);
1267 list_add(&tsk->cg_list, &newcg->tasks);
1268 }
1269 write_unlock(&css_set_lock);
1270
1271 for_each_subsys(root, ss) {
1272 if (ss->attach)
1273 ss->attach(ss, cgrp, oldcgrp, tsk);
1274 }
1275 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1276 synchronize_rcu();
1277 put_css_set(cg);
1278 return 0;
1279 }
1280
1281 /*
1282 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
1283 * cgroup_mutex, may take task_lock of task
1284 */
1285 static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
1286 {
1287 pid_t pid;
1288 struct task_struct *tsk;
1289 int ret;
1290
1291 if (sscanf(pidbuf, "%d", &pid) != 1)
1292 return -EIO;
1293
1294 if (pid) {
1295 rcu_read_lock();
1296 tsk = find_task_by_vpid(pid);
1297 if (!tsk || tsk->flags & PF_EXITING) {
1298 rcu_read_unlock();
1299 return -ESRCH;
1300 }
1301 get_task_struct(tsk);
1302 rcu_read_unlock();
1303
1304 if ((current->euid) && (current->euid != tsk->uid)
1305 && (current->euid != tsk->suid)) {
1306 put_task_struct(tsk);
1307 return -EACCES;
1308 }
1309 } else {
1310 tsk = current;
1311 get_task_struct(tsk);
1312 }
1313
1314 ret = cgroup_attach_task(cgrp, tsk);
1315 put_task_struct(tsk);
1316 return ret;
1317 }
1318
1319 /* The various types of files and directories in a cgroup file system */
1320 enum cgroup_filetype {
1321 FILE_ROOT,
1322 FILE_DIR,
1323 FILE_TASKLIST,
1324 FILE_NOTIFY_ON_RELEASE,
1325 FILE_RELEASE_AGENT,
1326 };
1327
1328 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
1329 struct file *file,
1330 const char __user *userbuf,
1331 size_t nbytes, loff_t *unused_ppos)
1332 {
1333 char buffer[64];
1334 int retval = 0;
1335 char *end;
1336
1337 if (!nbytes)
1338 return -EINVAL;
1339 if (nbytes >= sizeof(buffer))
1340 return -E2BIG;
1341 if (copy_from_user(buffer, userbuf, nbytes))
1342 return -EFAULT;
1343
1344 buffer[nbytes] = 0; /* nul-terminate */
1345 strstrip(buffer);
1346 if (cft->write_u64) {
1347 u64 val = simple_strtoull(buffer, &end, 0);
1348 if (*end)
1349 return -EINVAL;
1350 retval = cft->write_u64(cgrp, cft, val);
1351 } else {
1352 s64 val = simple_strtoll(buffer, &end, 0);
1353 if (*end)
1354 return -EINVAL;
1355 retval = cft->write_s64(cgrp, cft, val);
1356 }
1357 if (!retval)
1358 retval = nbytes;
1359 return retval;
1360 }
1361
1362 static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
1363 struct cftype *cft,
1364 struct file *file,
1365 const char __user *userbuf,
1366 size_t nbytes, loff_t *unused_ppos)
1367 {
1368 enum cgroup_filetype type = cft->private;
1369 char *buffer;
1370 int retval = 0;
1371
1372 if (nbytes >= PATH_MAX)
1373 return -E2BIG;
1374
1375 /* +1 for nul-terminator */
1376 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1377 if (buffer == NULL)
1378 return -ENOMEM;
1379
1380 if (copy_from_user(buffer, userbuf, nbytes)) {
1381 retval = -EFAULT;
1382 goto out1;
1383 }
1384 buffer[nbytes] = 0; /* nul-terminate */
1385 strstrip(buffer); /* strip -just- trailing whitespace */
1386
1387 mutex_lock(&cgroup_mutex);
1388
1389 /*
1390 * This was already checked for in cgroup_file_write(), but
1391 * check again now we're holding cgroup_mutex.
1392 */
1393 if (cgroup_is_removed(cgrp)) {
1394 retval = -ENODEV;
1395 goto out2;
1396 }
1397
1398 switch (type) {
1399 case FILE_TASKLIST:
1400 retval = attach_task_by_pid(cgrp, buffer);
1401 break;
1402 case FILE_NOTIFY_ON_RELEASE:
1403 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
1404 if (simple_strtoul(buffer, NULL, 10) != 0)
1405 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
1406 else
1407 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
1408 break;
1409 case FILE_RELEASE_AGENT:
1410 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1411 strcpy(cgrp->root->release_agent_path, buffer);
1412 break;
1413 default:
1414 retval = -EINVAL;
1415 goto out2;
1416 }
1417
1418 if (retval == 0)
1419 retval = nbytes;
1420 out2:
1421 mutex_unlock(&cgroup_mutex);
1422 out1:
1423 kfree(buffer);
1424 return retval;
1425 }
1426
1427 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1428 size_t nbytes, loff_t *ppos)
1429 {
1430 struct cftype *cft = __d_cft(file->f_dentry);
1431 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1432
1433 if (!cft || cgroup_is_removed(cgrp))
1434 return -ENODEV;
1435 if (cft->write)
1436 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1437 if (cft->write_u64 || cft->write_s64)
1438 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
1439 if (cft->trigger) {
1440 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1441 return ret ? ret : nbytes;
1442 }
1443 return -EINVAL;
1444 }
1445
1446 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1447 struct file *file,
1448 char __user *buf, size_t nbytes,
1449 loff_t *ppos)
1450 {
1451 char tmp[64];
1452 u64 val = cft->read_u64(cgrp, cft);
1453 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1454
1455 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1456 }
1457
1458 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1459 struct file *file,
1460 char __user *buf, size_t nbytes,
1461 loff_t *ppos)
1462 {
1463 char tmp[64];
1464 s64 val = cft->read_s64(cgrp, cft);
1465 int len = sprintf(tmp, "%lld\n", (long long) val);
1466
1467 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1468 }
1469
1470 static ssize_t cgroup_common_file_read(struct cgroup *cgrp,
1471 struct cftype *cft,
1472 struct file *file,
1473 char __user *buf,
1474 size_t nbytes, loff_t *ppos)
1475 {
1476 enum cgroup_filetype type = cft->private;
1477 char *page;
1478 ssize_t retval = 0;
1479 char *s;
1480
1481 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1482 return -ENOMEM;
1483
1484 s = page;
1485
1486 switch (type) {
1487 case FILE_RELEASE_AGENT:
1488 {
1489 struct cgroupfs_root *root;
1490 size_t n;
1491 mutex_lock(&cgroup_mutex);
1492 root = cgrp->root;
1493 n = strnlen(root->release_agent_path,
1494 sizeof(root->release_agent_path));
1495 n = min(n, (size_t) PAGE_SIZE);
1496 strncpy(s, root->release_agent_path, n);
1497 mutex_unlock(&cgroup_mutex);
1498 s += n;
1499 break;
1500 }
1501 default:
1502 retval = -EINVAL;
1503 goto out;
1504 }
1505 *s++ = '\n';
1506
1507 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1508 out:
1509 free_page((unsigned long)page);
1510 return retval;
1511 }
1512
1513 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1514 size_t nbytes, loff_t *ppos)
1515 {
1516 struct cftype *cft = __d_cft(file->f_dentry);
1517 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1518
1519 if (!cft || cgroup_is_removed(cgrp))
1520 return -ENODEV;
1521
1522 if (cft->read)
1523 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1524 if (cft->read_u64)
1525 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
1526 if (cft->read_s64)
1527 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
1528 return -EINVAL;
1529 }
1530
1531 /*
1532 * seqfile ops/methods for returning structured data. Currently just
1533 * supports string->u64 maps, but can be extended in future.
1534 */
1535
1536 struct cgroup_seqfile_state {
1537 struct cftype *cft;
1538 struct cgroup *cgroup;
1539 };
1540
1541 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1542 {
1543 struct seq_file *sf = cb->state;
1544 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1545 }
1546
1547 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1548 {
1549 struct cgroup_seqfile_state *state = m->private;
1550 struct cftype *cft = state->cft;
1551 struct cgroup_map_cb cb = {
1552 .fill = cgroup_map_add,
1553 .state = m,
1554 };
1555 return cft->read_map(state->cgroup, cft, &cb);
1556 }
1557
1558 int cgroup_seqfile_release(struct inode *inode, struct file *file)
1559 {
1560 struct seq_file *seq = file->private_data;
1561 kfree(seq->private);
1562 return single_release(inode, file);
1563 }
1564
1565 static struct file_operations cgroup_seqfile_operations = {
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = cgroup_seqfile_release,
1569 };
1570
1571 static int cgroup_file_open(struct inode *inode, struct file *file)
1572 {
1573 int err;
1574 struct cftype *cft;
1575
1576 err = generic_file_open(inode, file);
1577 if (err)
1578 return err;
1579
1580 cft = __d_cft(file->f_dentry);
1581 if (!cft)
1582 return -ENODEV;
1583 if (cft->read_map) {
1584 struct cgroup_seqfile_state *state =
1585 kzalloc(sizeof(*state), GFP_USER);
1586 if (!state)
1587 return -ENOMEM;
1588 state->cft = cft;
1589 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1590 file->f_op = &cgroup_seqfile_operations;
1591 err = single_open(file, cgroup_seqfile_show, state);
1592 if (err < 0)
1593 kfree(state);
1594 } else if (cft->open)
1595 err = cft->open(inode, file);
1596 else
1597 err = 0;
1598
1599 return err;
1600 }
1601
1602 static int cgroup_file_release(struct inode *inode, struct file *file)
1603 {
1604 struct cftype *cft = __d_cft(file->f_dentry);
1605 if (cft->release)
1606 return cft->release(inode, file);
1607 return 0;
1608 }
1609
1610 /*
1611 * cgroup_rename - Only allow simple rename of directories in place.
1612 */
1613 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1614 struct inode *new_dir, struct dentry *new_dentry)
1615 {
1616 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1617 return -ENOTDIR;
1618 if (new_dentry->d_inode)
1619 return -EEXIST;
1620 if (old_dir != new_dir)
1621 return -EIO;
1622 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1623 }
1624
1625 static struct file_operations cgroup_file_operations = {
1626 .read = cgroup_file_read,
1627 .write = cgroup_file_write,
1628 .llseek = generic_file_llseek,
1629 .open = cgroup_file_open,
1630 .release = cgroup_file_release,
1631 };
1632
1633 static struct inode_operations cgroup_dir_inode_operations = {
1634 .lookup = simple_lookup,
1635 .mkdir = cgroup_mkdir,
1636 .rmdir = cgroup_rmdir,
1637 .rename = cgroup_rename,
1638 };
1639
1640 static int cgroup_create_file(struct dentry *dentry, int mode,
1641 struct super_block *sb)
1642 {
1643 static struct dentry_operations cgroup_dops = {
1644 .d_iput = cgroup_diput,
1645 };
1646
1647 struct inode *inode;
1648
1649 if (!dentry)
1650 return -ENOENT;
1651 if (dentry->d_inode)
1652 return -EEXIST;
1653
1654 inode = cgroup_new_inode(mode, sb);
1655 if (!inode)
1656 return -ENOMEM;
1657
1658 if (S_ISDIR(mode)) {
1659 inode->i_op = &cgroup_dir_inode_operations;
1660 inode->i_fop = &simple_dir_operations;
1661
1662 /* start off with i_nlink == 2 (for "." entry) */
1663 inc_nlink(inode);
1664
1665 /* start with the directory inode held, so that we can
1666 * populate it without racing with another mkdir */
1667 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1668 } else if (S_ISREG(mode)) {
1669 inode->i_size = 0;
1670 inode->i_fop = &cgroup_file_operations;
1671 }
1672 dentry->d_op = &cgroup_dops;
1673 d_instantiate(dentry, inode);
1674 dget(dentry); /* Extra count - pin the dentry in core */
1675 return 0;
1676 }
1677
1678 /*
1679 * cgroup_create_dir - create a directory for an object.
1680 * @cgrp: the cgroup we create the directory for. It must have a valid
1681 * ->parent field. And we are going to fill its ->dentry field.
1682 * @dentry: dentry of the new cgroup
1683 * @mode: mode to set on new directory.
1684 */
1685 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1686 int mode)
1687 {
1688 struct dentry *parent;
1689 int error = 0;
1690
1691 parent = cgrp->parent->dentry;
1692 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
1693 if (!error) {
1694 dentry->d_fsdata = cgrp;
1695 inc_nlink(parent->d_inode);
1696 cgrp->dentry = dentry;
1697 dget(dentry);
1698 }
1699 dput(dentry);
1700
1701 return error;
1702 }
1703
1704 int cgroup_add_file(struct cgroup *cgrp,
1705 struct cgroup_subsys *subsys,
1706 const struct cftype *cft)
1707 {
1708 struct dentry *dir = cgrp->dentry;
1709 struct dentry *dentry;
1710 int error;
1711
1712 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1713 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
1714 strcpy(name, subsys->name);
1715 strcat(name, ".");
1716 }
1717 strcat(name, cft->name);
1718 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1719 dentry = lookup_one_len(name, dir, strlen(name));
1720 if (!IS_ERR(dentry)) {
1721 error = cgroup_create_file(dentry, 0644 | S_IFREG,
1722 cgrp->root->sb);
1723 if (!error)
1724 dentry->d_fsdata = (void *)cft;
1725 dput(dentry);
1726 } else
1727 error = PTR_ERR(dentry);
1728 return error;
1729 }
1730
1731 int cgroup_add_files(struct cgroup *cgrp,
1732 struct cgroup_subsys *subsys,
1733 const struct cftype cft[],
1734 int count)
1735 {
1736 int i, err;
1737 for (i = 0; i < count; i++) {
1738 err = cgroup_add_file(cgrp, subsys, &cft[i]);
1739 if (err)
1740 return err;
1741 }
1742 return 0;
1743 }
1744
1745 /**
1746 * cgroup_task_count - count the number of tasks in a cgroup.
1747 * @cgrp: the cgroup in question
1748 *
1749 * Return the number of tasks in the cgroup.
1750 */
1751 int cgroup_task_count(const struct cgroup *cgrp)
1752 {
1753 int count = 0;
1754 struct list_head *l;
1755
1756 read_lock(&css_set_lock);
1757 l = cgrp->css_sets.next;
1758 while (l != &cgrp->css_sets) {
1759 struct cg_cgroup_link *link =
1760 list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1761 count += atomic_read(&link->cg->ref.refcount);
1762 l = l->next;
1763 }
1764 read_unlock(&css_set_lock);
1765 return count;
1766 }
1767
1768 /*
1769 * Advance a list_head iterator. The iterator should be positioned at
1770 * the start of a css_set
1771 */
1772 static void cgroup_advance_iter(struct cgroup *cgrp,
1773 struct cgroup_iter *it)
1774 {
1775 struct list_head *l = it->cg_link;
1776 struct cg_cgroup_link *link;
1777 struct css_set *cg;
1778
1779 /* Advance to the next non-empty css_set */
1780 do {
1781 l = l->next;
1782 if (l == &cgrp->css_sets) {
1783 it->cg_link = NULL;
1784 return;
1785 }
1786 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1787 cg = link->cg;
1788 } while (list_empty(&cg->tasks));
1789 it->cg_link = l;
1790 it->task = cg->tasks.next;
1791 }
1792
1793 /*
1794 * To reduce the fork() overhead for systems that are not actually
1795 * using their cgroups capability, we don't maintain the lists running
1796 * through each css_set to its tasks until we see the list actually
1797 * used - in other words after the first call to cgroup_iter_start().
1798 *
1799 * The tasklist_lock is not held here, as do_each_thread() and
1800 * while_each_thread() are protected by RCU.
1801 */
1802 static void cgroup_enable_task_cg_lists(void)
1803 {
1804 struct task_struct *p, *g;
1805 write_lock(&css_set_lock);
1806 use_task_css_set_links = 1;
1807 do_each_thread(g, p) {
1808 task_lock(p);
1809 /*
1810 * We should check if the process is exiting, otherwise
1811 * it will race with cgroup_exit() in that the list
1812 * entry won't be deleted though the process has exited.
1813 */
1814 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
1815 list_add(&p->cg_list, &p->cgroups->tasks);
1816 task_unlock(p);
1817 } while_each_thread(g, p);
1818 write_unlock(&css_set_lock);
1819 }
1820
1821 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
1822 {
1823 /*
1824 * The first time anyone tries to iterate across a cgroup,
1825 * we need to enable the list linking each css_set to its
1826 * tasks, and fix up all existing tasks.
1827 */
1828 if (!use_task_css_set_links)
1829 cgroup_enable_task_cg_lists();
1830
1831 read_lock(&css_set_lock);
1832 it->cg_link = &cgrp->css_sets;
1833 cgroup_advance_iter(cgrp, it);
1834 }
1835
1836 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
1837 struct cgroup_iter *it)
1838 {
1839 struct task_struct *res;
1840 struct list_head *l = it->task;
1841
1842 /* If the iterator cg is NULL, we have no tasks */
1843 if (!it->cg_link)
1844 return NULL;
1845 res = list_entry(l, struct task_struct, cg_list);
1846 /* Advance iterator to find next entry */
1847 l = l->next;
1848 if (l == &res->cgroups->tasks) {
1849 /* We reached the end of this task list - move on to
1850 * the next cg_cgroup_link */
1851 cgroup_advance_iter(cgrp, it);
1852 } else {
1853 it->task = l;
1854 }
1855 return res;
1856 }
1857
1858 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
1859 {
1860 read_unlock(&css_set_lock);
1861 }
1862
1863 static inline int started_after_time(struct task_struct *t1,
1864 struct timespec *time,
1865 struct task_struct *t2)
1866 {
1867 int start_diff = timespec_compare(&t1->start_time, time);
1868 if (start_diff > 0) {
1869 return 1;
1870 } else if (start_diff < 0) {
1871 return 0;
1872 } else {
1873 /*
1874 * Arbitrarily, if two processes started at the same
1875 * time, we'll say that the lower pointer value
1876 * started first. Note that t2 may have exited by now
1877 * so this may not be a valid pointer any longer, but
1878 * that's fine - it still serves to distinguish
1879 * between two tasks started (effectively) simultaneously.
1880 */
1881 return t1 > t2;
1882 }
1883 }
1884
1885 /*
1886 * This function is a callback from heap_insert() and is used to order
1887 * the heap.
1888 * In this case we order the heap in descending task start time.
1889 */
1890 static inline int started_after(void *p1, void *p2)
1891 {
1892 struct task_struct *t1 = p1;
1893 struct task_struct *t2 = p2;
1894 return started_after_time(t1, &t2->start_time, t2);
1895 }
1896
1897 /**
1898 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1899 * @scan: struct cgroup_scanner containing arguments for the scan
1900 *
1901 * Arguments include pointers to callback functions test_task() and
1902 * process_task().
1903 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1904 * and if it returns true, call process_task() for it also.
1905 * The test_task pointer may be NULL, meaning always true (select all tasks).
1906 * Effectively duplicates cgroup_iter_{start,next,end}()
1907 * but does not lock css_set_lock for the call to process_task().
1908 * The struct cgroup_scanner may be embedded in any structure of the caller's
1909 * creation.
1910 * It is guaranteed that process_task() will act on every task that
1911 * is a member of the cgroup for the duration of this call. This
1912 * function may or may not call process_task() for tasks that exit
1913 * or move to a different cgroup during the call, or are forked or
1914 * move into the cgroup during the call.
1915 *
1916 * Note that test_task() may be called with locks held, and may in some
1917 * situations be called multiple times for the same task, so it should
1918 * be cheap.
1919 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1920 * pre-allocated and will be used for heap operations (and its "gt" member will
1921 * be overwritten), else a temporary heap will be used (allocation of which
1922 * may cause this function to fail).
1923 */
1924 int cgroup_scan_tasks(struct cgroup_scanner *scan)
1925 {
1926 int retval, i;
1927 struct cgroup_iter it;
1928 struct task_struct *p, *dropped;
1929 /* Never dereference latest_task, since it's not refcounted */
1930 struct task_struct *latest_task = NULL;
1931 struct ptr_heap tmp_heap;
1932 struct ptr_heap *heap;
1933 struct timespec latest_time = { 0, 0 };
1934
1935 if (scan->heap) {
1936 /* The caller supplied our heap and pre-allocated its memory */
1937 heap = scan->heap;
1938 heap->gt = &started_after;
1939 } else {
1940 /* We need to allocate our own heap memory */
1941 heap = &tmp_heap;
1942 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1943 if (retval)
1944 /* cannot allocate the heap */
1945 return retval;
1946 }
1947
1948 again:
1949 /*
1950 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1951 * to determine which are of interest, and using the scanner's
1952 * "process_task" callback to process any of them that need an update.
1953 * Since we don't want to hold any locks during the task updates,
1954 * gather tasks to be processed in a heap structure.
1955 * The heap is sorted by descending task start time.
1956 * If the statically-sized heap fills up, we overflow tasks that
1957 * started later, and in future iterations only consider tasks that
1958 * started after the latest task in the previous pass. This
1959 * guarantees forward progress and that we don't miss any tasks.
1960 */
1961 heap->size = 0;
1962 cgroup_iter_start(scan->cg, &it);
1963 while ((p = cgroup_iter_next(scan->cg, &it))) {
1964 /*
1965 * Only affect tasks that qualify per the caller's callback,
1966 * if he provided one
1967 */
1968 if (scan->test_task && !scan->test_task(p, scan))
1969 continue;
1970 /*
1971 * Only process tasks that started after the last task
1972 * we processed
1973 */
1974 if (!started_after_time(p, &latest_time, latest_task))
1975 continue;
1976 dropped = heap_insert(heap, p);
1977 if (dropped == NULL) {
1978 /*
1979 * The new task was inserted; the heap wasn't
1980 * previously full
1981 */
1982 get_task_struct(p);
1983 } else if (dropped != p) {
1984 /*
1985 * The new task was inserted, and pushed out a
1986 * different task
1987 */
1988 get_task_struct(p);
1989 put_task_struct(dropped);
1990 }
1991 /*
1992 * Else the new task was newer than anything already in
1993 * the heap and wasn't inserted
1994 */
1995 }
1996 cgroup_iter_end(scan->cg, &it);
1997
1998 if (heap->size) {
1999 for (i = 0; i < heap->size; i++) {
2000 struct task_struct *q = heap->ptrs[i];
2001 if (i == 0) {
2002 latest_time = q->start_time;
2003 latest_task = q;
2004 }
2005 /* Process the task per the caller's callback */
2006 scan->process_task(q, scan);
2007 put_task_struct(q);
2008 }
2009 /*
2010 * If we had to process any tasks at all, scan again
2011 * in case some of them were in the middle of forking
2012 * children that didn't get processed.
2013 * Not the most efficient way to do it, but it avoids
2014 * having to take callback_mutex in the fork path
2015 */
2016 goto again;
2017 }
2018 if (heap == &tmp_heap)
2019 heap_free(&tmp_heap);
2020 return 0;
2021 }
2022
2023 /*
2024 * Stuff for reading the 'tasks' file.
2025 *
2026 * Reading this file can return large amounts of data if a cgroup has
2027 * *lots* of attached tasks. So it may need several calls to read(),
2028 * but we cannot guarantee that the information we produce is correct
2029 * unless we produce it entirely atomically.
2030 *
2031 * Upon tasks file open(), a struct ctr_struct is allocated, that
2032 * will have a pointer to an array (also allocated here). The struct
2033 * ctr_struct * is stored in file->private_data. Its resources will
2034 * be freed by release() when the file is closed. The array is used
2035 * to sprintf the PIDs and then used by read().
2036 */
2037 struct ctr_struct {
2038 char *buf;
2039 int bufsz;
2040 };
2041
2042 /*
2043 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2044 * 'cgrp'. Return actual number of pids loaded. No need to
2045 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2046 * read section, so the css_set can't go away, and is
2047 * immutable after creation.
2048 */
2049 static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
2050 {
2051 int n = 0;
2052 struct cgroup_iter it;
2053 struct task_struct *tsk;
2054 cgroup_iter_start(cgrp, &it);
2055 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2056 if (unlikely(n == npids))
2057 break;
2058 pidarray[n++] = task_pid_vnr(tsk);
2059 }
2060 cgroup_iter_end(cgrp, &it);
2061 return n;
2062 }
2063
2064 /**
2065 * cgroupstats_build - build and fill cgroupstats
2066 * @stats: cgroupstats to fill information into
2067 * @dentry: A dentry entry belonging to the cgroup for which stats have
2068 * been requested.
2069 *
2070 * Build and fill cgroupstats so that taskstats can export it to user
2071 * space.
2072 */
2073 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2074 {
2075 int ret = -EINVAL;
2076 struct cgroup *cgrp;
2077 struct cgroup_iter it;
2078 struct task_struct *tsk;
2079 /*
2080 * Validate dentry by checking the superblock operations
2081 */
2082 if (dentry->d_sb->s_op != &cgroup_ops)
2083 goto err;
2084
2085 ret = 0;
2086 cgrp = dentry->d_fsdata;
2087 rcu_read_lock();
2088
2089 cgroup_iter_start(cgrp, &it);
2090 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2091 switch (tsk->state) {
2092 case TASK_RUNNING:
2093 stats->nr_running++;
2094 break;
2095 case TASK_INTERRUPTIBLE:
2096 stats->nr_sleeping++;
2097 break;
2098 case TASK_UNINTERRUPTIBLE:
2099 stats->nr_uninterruptible++;
2100 break;
2101 case TASK_STOPPED:
2102 stats->nr_stopped++;
2103 break;
2104 default:
2105 if (delayacct_is_task_waiting_on_io(tsk))
2106 stats->nr_io_wait++;
2107 break;
2108 }
2109 }
2110 cgroup_iter_end(cgrp, &it);
2111
2112 rcu_read_unlock();
2113 err:
2114 return ret;
2115 }
2116
2117 static int cmppid(const void *a, const void *b)
2118 {
2119 return *(pid_t *)a - *(pid_t *)b;
2120 }
2121
2122 /*
2123 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2124 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2125 * count 'cnt' of how many chars would be written if buf were large enough.
2126 */
2127 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2128 {
2129 int cnt = 0;
2130 int i;
2131
2132 for (i = 0; i < npids; i++)
2133 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2134 return cnt;
2135 }
2136
2137 /*
2138 * Handle an open on 'tasks' file. Prepare a buffer listing the
2139 * process id's of tasks currently attached to the cgroup being opened.
2140 *
2141 * Does not require any specific cgroup mutexes, and does not take any.
2142 */
2143 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2144 {
2145 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2146 struct ctr_struct *ctr;
2147 pid_t *pidarray;
2148 int npids;
2149 char c;
2150
2151 if (!(file->f_mode & FMODE_READ))
2152 return 0;
2153
2154 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2155 if (!ctr)
2156 goto err0;
2157
2158 /*
2159 * If cgroup gets more users after we read count, we won't have
2160 * enough space - tough. This race is indistinguishable to the
2161 * caller from the case that the additional cgroup users didn't
2162 * show up until sometime later on.
2163 */
2164 npids = cgroup_task_count(cgrp);
2165 if (npids) {
2166 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2167 if (!pidarray)
2168 goto err1;
2169
2170 npids = pid_array_load(pidarray, npids, cgrp);
2171 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2172
2173 /* Call pid_array_to_buf() twice, first just to get bufsz */
2174 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2175 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2176 if (!ctr->buf)
2177 goto err2;
2178 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2179
2180 kfree(pidarray);
2181 } else {
2182 ctr->buf = NULL;
2183 ctr->bufsz = 0;
2184 }
2185 file->private_data = ctr;
2186 return 0;
2187
2188 err2:
2189 kfree(pidarray);
2190 err1:
2191 kfree(ctr);
2192 err0:
2193 return -ENOMEM;
2194 }
2195
2196 static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
2197 struct cftype *cft,
2198 struct file *file, char __user *buf,
2199 size_t nbytes, loff_t *ppos)
2200 {
2201 struct ctr_struct *ctr = file->private_data;
2202
2203 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2204 }
2205
2206 static int cgroup_tasks_release(struct inode *unused_inode,
2207 struct file *file)
2208 {
2209 struct ctr_struct *ctr;
2210
2211 if (file->f_mode & FMODE_READ) {
2212 ctr = file->private_data;
2213 kfree(ctr->buf);
2214 kfree(ctr);
2215 }
2216 return 0;
2217 }
2218
2219 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2220 struct cftype *cft)
2221 {
2222 return notify_on_release(cgrp);
2223 }
2224
2225 /*
2226 * for the common functions, 'private' gives the type of file
2227 */
2228 static struct cftype files[] = {
2229 {
2230 .name = "tasks",
2231 .open = cgroup_tasks_open,
2232 .read = cgroup_tasks_read,
2233 .write = cgroup_common_file_write,
2234 .release = cgroup_tasks_release,
2235 .private = FILE_TASKLIST,
2236 },
2237
2238 {
2239 .name = "notify_on_release",
2240 .read_u64 = cgroup_read_notify_on_release,
2241 .write = cgroup_common_file_write,
2242 .private = FILE_NOTIFY_ON_RELEASE,
2243 },
2244 };
2245
2246 static struct cftype cft_release_agent = {
2247 .name = "release_agent",
2248 .read = cgroup_common_file_read,
2249 .write = cgroup_common_file_write,
2250 .private = FILE_RELEASE_AGENT,
2251 };
2252
2253 static int cgroup_populate_dir(struct cgroup *cgrp)
2254 {
2255 int err;
2256 struct cgroup_subsys *ss;
2257
2258 /* First clear out any existing files */
2259 cgroup_clear_directory(cgrp->dentry);
2260
2261 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2262 if (err < 0)
2263 return err;
2264
2265 if (cgrp == cgrp->top_cgroup) {
2266 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2267 return err;
2268 }
2269
2270 for_each_subsys(cgrp->root, ss) {
2271 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2272 return err;
2273 }
2274
2275 return 0;
2276 }
2277
2278 static void init_cgroup_css(struct cgroup_subsys_state *css,
2279 struct cgroup_subsys *ss,
2280 struct cgroup *cgrp)
2281 {
2282 css->cgroup = cgrp;
2283 atomic_set(&css->refcnt, 0);
2284 css->flags = 0;
2285 if (cgrp == dummytop)
2286 set_bit(CSS_ROOT, &css->flags);
2287 BUG_ON(cgrp->subsys[ss->subsys_id]);
2288 cgrp->subsys[ss->subsys_id] = css;
2289 }
2290
2291 /*
2292 * cgroup_create - create a cgroup
2293 * @parent: cgroup that will be parent of the new cgroup
2294 * @dentry: dentry of the new cgroup
2295 * @mode: mode to set on new inode
2296 *
2297 * Must be called with the mutex on the parent inode held
2298 */
2299 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2300 int mode)
2301 {
2302 struct cgroup *cgrp;
2303 struct cgroupfs_root *root = parent->root;
2304 int err = 0;
2305 struct cgroup_subsys *ss;
2306 struct super_block *sb = root->sb;
2307
2308 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2309 if (!cgrp)
2310 return -ENOMEM;
2311
2312 /* Grab a reference on the superblock so the hierarchy doesn't
2313 * get deleted on unmount if there are child cgroups. This
2314 * can be done outside cgroup_mutex, since the sb can't
2315 * disappear while someone has an open control file on the
2316 * fs */
2317 atomic_inc(&sb->s_active);
2318
2319 mutex_lock(&cgroup_mutex);
2320
2321 INIT_LIST_HEAD(&cgrp->sibling);
2322 INIT_LIST_HEAD(&cgrp->children);
2323 INIT_LIST_HEAD(&cgrp->css_sets);
2324 INIT_LIST_HEAD(&cgrp->release_list);
2325
2326 cgrp->parent = parent;
2327 cgrp->root = parent->root;
2328 cgrp->top_cgroup = parent->top_cgroup;
2329
2330 if (notify_on_release(parent))
2331 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2332
2333 for_each_subsys(root, ss) {
2334 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
2335 if (IS_ERR(css)) {
2336 err = PTR_ERR(css);
2337 goto err_destroy;
2338 }
2339 init_cgroup_css(css, ss, cgrp);
2340 }
2341
2342 list_add(&cgrp->sibling, &cgrp->parent->children);
2343 root->number_of_cgroups++;
2344
2345 err = cgroup_create_dir(cgrp, dentry, mode);
2346 if (err < 0)
2347 goto err_remove;
2348
2349 /* The cgroup directory was pre-locked for us */
2350 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
2351
2352 err = cgroup_populate_dir(cgrp);
2353 /* If err < 0, we have a half-filled directory - oh well ;) */
2354
2355 mutex_unlock(&cgroup_mutex);
2356 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
2357
2358 return 0;
2359
2360 err_remove:
2361
2362 list_del(&cgrp->sibling);
2363 root->number_of_cgroups--;
2364
2365 err_destroy:
2366
2367 for_each_subsys(root, ss) {
2368 if (cgrp->subsys[ss->subsys_id])
2369 ss->destroy(ss, cgrp);
2370 }
2371
2372 mutex_unlock(&cgroup_mutex);
2373
2374 /* Release the reference count that we took on the superblock */
2375 deactivate_super(sb);
2376
2377 kfree(cgrp);
2378 return err;
2379 }
2380
2381 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2382 {
2383 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2384
2385 /* the vfs holds inode->i_mutex already */
2386 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2387 }
2388
2389 static inline int cgroup_has_css_refs(struct cgroup *cgrp)
2390 {
2391 /* Check the reference count on each subsystem. Since we
2392 * already established that there are no tasks in the
2393 * cgroup, if the css refcount is also 0, then there should
2394 * be no outstanding references, so the subsystem is safe to
2395 * destroy. We scan across all subsystems rather than using
2396 * the per-hierarchy linked list of mounted subsystems since
2397 * we can be called via check_for_release() with no
2398 * synchronization other than RCU, and the subsystem linked
2399 * list isn't RCU-safe */
2400 int i;
2401 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2402 struct cgroup_subsys *ss = subsys[i];
2403 struct cgroup_subsys_state *css;
2404 /* Skip subsystems not in this hierarchy */
2405 if (ss->root != cgrp->root)
2406 continue;
2407 css = cgrp->subsys[ss->subsys_id];
2408 /* When called from check_for_release() it's possible
2409 * that by this point the cgroup has been removed
2410 * and the css deleted. But a false-positive doesn't
2411 * matter, since it can only happen if the cgroup
2412 * has been deleted and hence no longer needs the
2413 * release agent to be called anyway. */
2414 if (css && atomic_read(&css->refcnt))
2415 return 1;
2416 }
2417 return 0;
2418 }
2419
2420 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2421 {
2422 struct cgroup *cgrp = dentry->d_fsdata;
2423 struct dentry *d;
2424 struct cgroup *parent;
2425 struct super_block *sb;
2426 struct cgroupfs_root *root;
2427
2428 /* the vfs holds both inode->i_mutex already */
2429
2430 mutex_lock(&cgroup_mutex);
2431 if (atomic_read(&cgrp->count) != 0) {
2432 mutex_unlock(&cgroup_mutex);
2433 return -EBUSY;
2434 }
2435 if (!list_empty(&cgrp->children)) {
2436 mutex_unlock(&cgroup_mutex);
2437 return -EBUSY;
2438 }
2439
2440 parent = cgrp->parent;
2441 root = cgrp->root;
2442 sb = root->sb;
2443
2444 /*
2445 * Call pre_destroy handlers of subsys. Notify subsystems
2446 * that rmdir() request comes.
2447 */
2448 cgroup_call_pre_destroy(cgrp);
2449
2450 if (cgroup_has_css_refs(cgrp)) {
2451 mutex_unlock(&cgroup_mutex);
2452 return -EBUSY;
2453 }
2454
2455 spin_lock(&release_list_lock);
2456 set_bit(CGRP_REMOVED, &cgrp->flags);
2457 if (!list_empty(&cgrp->release_list))
2458 list_del(&cgrp->release_list);
2459 spin_unlock(&release_list_lock);
2460 /* delete my sibling from parent->children */
2461 list_del(&cgrp->sibling);
2462 spin_lock(&cgrp->dentry->d_lock);
2463 d = dget(cgrp->dentry);
2464 cgrp->dentry = NULL;
2465 spin_unlock(&d->d_lock);
2466
2467 cgroup_d_remove_dir(d);
2468 dput(d);
2469
2470 set_bit(CGRP_RELEASABLE, &parent->flags);
2471 check_for_release(parent);
2472
2473 mutex_unlock(&cgroup_mutex);
2474 return 0;
2475 }
2476
2477 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
2478 {
2479 struct cgroup_subsys_state *css;
2480 struct list_head *l;
2481
2482 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
2483
2484 /* Create the top cgroup state for this subsystem */
2485 ss->root = &rootnode;
2486 css = ss->create(ss, dummytop);
2487 /* We don't handle early failures gracefully */
2488 BUG_ON(IS_ERR(css));
2489 init_cgroup_css(css, ss, dummytop);
2490
2491 /* Update all cgroup groups to contain a subsys
2492 * pointer to this state - since the subsystem is
2493 * newly registered, all tasks and hence all cgroup
2494 * groups are in the subsystem's top cgroup. */
2495 write_lock(&css_set_lock);
2496 l = &init_css_set.list;
2497 do {
2498 struct css_set *cg =
2499 list_entry(l, struct css_set, list);
2500 cg->subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
2501 l = l->next;
2502 } while (l != &init_css_set.list);
2503 write_unlock(&css_set_lock);
2504
2505 /* If this subsystem requested that it be notified with fork
2506 * events, we should send it one now for every process in the
2507 * system */
2508 if (ss->fork) {
2509 struct task_struct *g, *p;
2510
2511 read_lock(&tasklist_lock);
2512 do_each_thread(g, p) {
2513 ss->fork(ss, p);
2514 } while_each_thread(g, p);
2515 read_unlock(&tasklist_lock);
2516 }
2517
2518 need_forkexit_callback |= ss->fork || ss->exit;
2519
2520 ss->active = 1;
2521 }
2522
2523 /**
2524 * cgroup_init_early - cgroup initialization at system boot
2525 *
2526 * Initialize cgroups at system boot, and initialize any
2527 * subsystems that request early init.
2528 */
2529 int __init cgroup_init_early(void)
2530 {
2531 int i;
2532 kref_init(&init_css_set.ref);
2533 kref_get(&init_css_set.ref);
2534 INIT_LIST_HEAD(&init_css_set.list);
2535 INIT_LIST_HEAD(&init_css_set.cg_links);
2536 INIT_LIST_HEAD(&init_css_set.tasks);
2537 INIT_HLIST_NODE(&init_css_set.hlist);
2538 css_set_count = 1;
2539 init_cgroup_root(&rootnode);
2540 list_add(&rootnode.root_list, &roots);
2541 root_count = 1;
2542 init_task.cgroups = &init_css_set;
2543
2544 init_css_set_link.cg = &init_css_set;
2545 list_add(&init_css_set_link.cgrp_link_list,
2546 &rootnode.top_cgroup.css_sets);
2547 list_add(&init_css_set_link.cg_link_list,
2548 &init_css_set.cg_links);
2549
2550 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2551 INIT_HLIST_HEAD(&css_set_table[i]);
2552
2553 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2554 struct cgroup_subsys *ss = subsys[i];
2555
2556 BUG_ON(!ss->name);
2557 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2558 BUG_ON(!ss->create);
2559 BUG_ON(!ss->destroy);
2560 if (ss->subsys_id != i) {
2561 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
2562 ss->name, ss->subsys_id);
2563 BUG();
2564 }
2565
2566 if (ss->early_init)
2567 cgroup_init_subsys(ss);
2568 }
2569 return 0;
2570 }
2571
2572 /**
2573 * cgroup_init - cgroup initialization
2574 *
2575 * Register cgroup filesystem and /proc file, and initialize
2576 * any subsystems that didn't request early init.
2577 */
2578 int __init cgroup_init(void)
2579 {
2580 int err;
2581 int i;
2582 struct hlist_head *hhead;
2583
2584 err = bdi_init(&cgroup_backing_dev_info);
2585 if (err)
2586 return err;
2587
2588 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2589 struct cgroup_subsys *ss = subsys[i];
2590 if (!ss->early_init)
2591 cgroup_init_subsys(ss);
2592 }
2593
2594 /* Add init_css_set to the hash table */
2595 hhead = css_set_hash(init_css_set.subsys);
2596 hlist_add_head(&init_css_set.hlist, hhead);
2597
2598 err = register_filesystem(&cgroup_fs_type);
2599 if (err < 0)
2600 goto out;
2601
2602 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
2603
2604 out:
2605 if (err)
2606 bdi_destroy(&cgroup_backing_dev_info);
2607
2608 return err;
2609 }
2610
2611 /*
2612 * proc_cgroup_show()
2613 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2614 * - Used for /proc/<pid>/cgroup.
2615 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2616 * doesn't really matter if tsk->cgroup changes after we read it,
2617 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
2618 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2619 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2620 * cgroup to top_cgroup.
2621 */
2622
2623 /* TODO: Use a proper seq_file iterator */
2624 static int proc_cgroup_show(struct seq_file *m, void *v)
2625 {
2626 struct pid *pid;
2627 struct task_struct *tsk;
2628 char *buf;
2629 int retval;
2630 struct cgroupfs_root *root;
2631
2632 retval = -ENOMEM;
2633 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2634 if (!buf)
2635 goto out;
2636
2637 retval = -ESRCH;
2638 pid = m->private;
2639 tsk = get_pid_task(pid, PIDTYPE_PID);
2640 if (!tsk)
2641 goto out_free;
2642
2643 retval = 0;
2644
2645 mutex_lock(&cgroup_mutex);
2646
2647 for_each_root(root) {
2648 struct cgroup_subsys *ss;
2649 struct cgroup *cgrp;
2650 int subsys_id;
2651 int count = 0;
2652
2653 /* Skip this hierarchy if it has no active subsystems */
2654 if (!root->actual_subsys_bits)
2655 continue;
2656 seq_printf(m, "%lu:", root->subsys_bits);
2657 for_each_subsys(root, ss)
2658 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2659 seq_putc(m, ':');
2660 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
2661 cgrp = task_cgroup(tsk, subsys_id);
2662 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
2663 if (retval < 0)
2664 goto out_unlock;
2665 seq_puts(m, buf);
2666 seq_putc(m, '\n');
2667 }
2668
2669 out_unlock:
2670 mutex_unlock(&cgroup_mutex);
2671 put_task_struct(tsk);
2672 out_free:
2673 kfree(buf);
2674 out:
2675 return retval;
2676 }
2677
2678 static int cgroup_open(struct inode *inode, struct file *file)
2679 {
2680 struct pid *pid = PROC_I(inode)->pid;
2681 return single_open(file, proc_cgroup_show, pid);
2682 }
2683
2684 struct file_operations proc_cgroup_operations = {
2685 .open = cgroup_open,
2686 .read = seq_read,
2687 .llseek = seq_lseek,
2688 .release = single_release,
2689 };
2690
2691 /* Display information about each subsystem and each hierarchy */
2692 static int proc_cgroupstats_show(struct seq_file *m, void *v)
2693 {
2694 int i;
2695
2696 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
2697 mutex_lock(&cgroup_mutex);
2698 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2699 struct cgroup_subsys *ss = subsys[i];
2700 seq_printf(m, "%s\t%lu\t%d\t%d\n",
2701 ss->name, ss->root->subsys_bits,
2702 ss->root->number_of_cgroups, !ss->disabled);
2703 }
2704 mutex_unlock(&cgroup_mutex);
2705 return 0;
2706 }
2707
2708 static int cgroupstats_open(struct inode *inode, struct file *file)
2709 {
2710 return single_open(file, proc_cgroupstats_show, NULL);
2711 }
2712
2713 static struct file_operations proc_cgroupstats_operations = {
2714 .open = cgroupstats_open,
2715 .read = seq_read,
2716 .llseek = seq_lseek,
2717 .release = single_release,
2718 };
2719
2720 /**
2721 * cgroup_fork - attach newly forked task to its parents cgroup.
2722 * @child: pointer to task_struct of forking parent process.
2723 *
2724 * Description: A task inherits its parent's cgroup at fork().
2725 *
2726 * A pointer to the shared css_set was automatically copied in
2727 * fork.c by dup_task_struct(). However, we ignore that copy, since
2728 * it was not made under the protection of RCU or cgroup_mutex, so
2729 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
2730 * have already changed current->cgroups, allowing the previously
2731 * referenced cgroup group to be removed and freed.
2732 *
2733 * At the point that cgroup_fork() is called, 'current' is the parent
2734 * task, and the passed argument 'child' points to the child task.
2735 */
2736 void cgroup_fork(struct task_struct *child)
2737 {
2738 task_lock(current);
2739 child->cgroups = current->cgroups;
2740 get_css_set(child->cgroups);
2741 task_unlock(current);
2742 INIT_LIST_HEAD(&child->cg_list);
2743 }
2744
2745 /**
2746 * cgroup_fork_callbacks - run fork callbacks
2747 * @child: the new task
2748 *
2749 * Called on a new task very soon before adding it to the
2750 * tasklist. No need to take any locks since no-one can
2751 * be operating on this task.
2752 */
2753 void cgroup_fork_callbacks(struct task_struct *child)
2754 {
2755 if (need_forkexit_callback) {
2756 int i;
2757 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2758 struct cgroup_subsys *ss = subsys[i];
2759 if (ss->fork)
2760 ss->fork(ss, child);
2761 }
2762 }
2763 }
2764
2765 /**
2766 * cgroup_post_fork - called on a new task after adding it to the task list
2767 * @child: the task in question
2768 *
2769 * Adds the task to the list running through its css_set if necessary.
2770 * Has to be after the task is visible on the task list in case we race
2771 * with the first call to cgroup_iter_start() - to guarantee that the
2772 * new task ends up on its list.
2773 */
2774 void cgroup_post_fork(struct task_struct *child)
2775 {
2776 if (use_task_css_set_links) {
2777 write_lock(&css_set_lock);
2778 if (list_empty(&child->cg_list))
2779 list_add(&child->cg_list, &child->cgroups->tasks);
2780 write_unlock(&css_set_lock);
2781 }
2782 }
2783 /**
2784 * cgroup_exit - detach cgroup from exiting task
2785 * @tsk: pointer to task_struct of exiting process
2786 * @run_callback: run exit callbacks?
2787 *
2788 * Description: Detach cgroup from @tsk and release it.
2789 *
2790 * Note that cgroups marked notify_on_release force every task in
2791 * them to take the global cgroup_mutex mutex when exiting.
2792 * This could impact scaling on very large systems. Be reluctant to
2793 * use notify_on_release cgroups where very high task exit scaling
2794 * is required on large systems.
2795 *
2796 * the_top_cgroup_hack:
2797 *
2798 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2799 *
2800 * We call cgroup_exit() while the task is still competent to
2801 * handle notify_on_release(), then leave the task attached to the
2802 * root cgroup in each hierarchy for the remainder of its exit.
2803 *
2804 * To do this properly, we would increment the reference count on
2805 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2806 * code we would add a second cgroup function call, to drop that
2807 * reference. This would just create an unnecessary hot spot on
2808 * the top_cgroup reference count, to no avail.
2809 *
2810 * Normally, holding a reference to a cgroup without bumping its
2811 * count is unsafe. The cgroup could go away, or someone could
2812 * attach us to a different cgroup, decrementing the count on
2813 * the first cgroup that we never incremented. But in this case,
2814 * top_cgroup isn't going away, and either task has PF_EXITING set,
2815 * which wards off any cgroup_attach_task() attempts, or task is a failed
2816 * fork, never visible to cgroup_attach_task.
2817 */
2818 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2819 {
2820 int i;
2821 struct css_set *cg;
2822
2823 if (run_callbacks && need_forkexit_callback) {
2824 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2825 struct cgroup_subsys *ss = subsys[i];
2826 if (ss->exit)
2827 ss->exit(ss, tsk);
2828 }
2829 }
2830
2831 /*
2832 * Unlink from the css_set task list if necessary.
2833 * Optimistically check cg_list before taking
2834 * css_set_lock
2835 */
2836 if (!list_empty(&tsk->cg_list)) {
2837 write_lock(&css_set_lock);
2838 if (!list_empty(&tsk->cg_list))
2839 list_del(&tsk->cg_list);
2840 write_unlock(&css_set_lock);
2841 }
2842
2843 /* Reassign the task to the init_css_set. */
2844 task_lock(tsk);
2845 cg = tsk->cgroups;
2846 tsk->cgroups = &init_css_set;
2847 task_unlock(tsk);
2848 if (cg)
2849 put_css_set_taskexit(cg);
2850 }
2851
2852 /**
2853 * cgroup_clone - clone the cgroup the given subsystem is attached to
2854 * @tsk: the task to be moved
2855 * @subsys: the given subsystem
2856 *
2857 * Duplicate the current cgroup in the hierarchy that the given
2858 * subsystem is attached to, and move this task into the new
2859 * child.
2860 */
2861 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2862 {
2863 struct dentry *dentry;
2864 int ret = 0;
2865 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2866 struct cgroup *parent, *child;
2867 struct inode *inode;
2868 struct css_set *cg;
2869 struct cgroupfs_root *root;
2870 struct cgroup_subsys *ss;
2871
2872 /* We shouldn't be called by an unregistered subsystem */
2873 BUG_ON(!subsys->active);
2874
2875 /* First figure out what hierarchy and cgroup we're dealing
2876 * with, and pin them so we can drop cgroup_mutex */
2877 mutex_lock(&cgroup_mutex);
2878 again:
2879 root = subsys->root;
2880 if (root == &rootnode) {
2881 printk(KERN_INFO
2882 "Not cloning cgroup for unused subsystem %s\n",
2883 subsys->name);
2884 mutex_unlock(&cgroup_mutex);
2885 return 0;
2886 }
2887 cg = tsk->cgroups;
2888 parent = task_cgroup(tsk, subsys->subsys_id);
2889
2890 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
2891
2892 /* Pin the hierarchy */
2893 atomic_inc(&parent->root->sb->s_active);
2894
2895 /* Keep the cgroup alive */
2896 get_css_set(cg);
2897 mutex_unlock(&cgroup_mutex);
2898
2899 /* Now do the VFS work to create a cgroup */
2900 inode = parent->dentry->d_inode;
2901
2902 /* Hold the parent directory mutex across this operation to
2903 * stop anyone else deleting the new cgroup */
2904 mutex_lock(&inode->i_mutex);
2905 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2906 if (IS_ERR(dentry)) {
2907 printk(KERN_INFO
2908 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
2909 PTR_ERR(dentry));
2910 ret = PTR_ERR(dentry);
2911 goto out_release;
2912 }
2913
2914 /* Create the cgroup directory, which also creates the cgroup */
2915 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
2916 child = __d_cgrp(dentry);
2917 dput(dentry);
2918 if (ret) {
2919 printk(KERN_INFO
2920 "Failed to create cgroup %s: %d\n", nodename,
2921 ret);
2922 goto out_release;
2923 }
2924
2925 if (!child) {
2926 printk(KERN_INFO
2927 "Couldn't find new cgroup %s\n", nodename);
2928 ret = -ENOMEM;
2929 goto out_release;
2930 }
2931
2932 /* The cgroup now exists. Retake cgroup_mutex and check
2933 * that we're still in the same state that we thought we
2934 * were. */
2935 mutex_lock(&cgroup_mutex);
2936 if ((root != subsys->root) ||
2937 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2938 /* Aargh, we raced ... */
2939 mutex_unlock(&inode->i_mutex);
2940 put_css_set(cg);
2941
2942 deactivate_super(parent->root->sb);
2943 /* The cgroup is still accessible in the VFS, but
2944 * we're not going to try to rmdir() it at this
2945 * point. */
2946 printk(KERN_INFO
2947 "Race in cgroup_clone() - leaking cgroup %s\n",
2948 nodename);
2949 goto again;
2950 }
2951
2952 /* do any required auto-setup */
2953 for_each_subsys(root, ss) {
2954 if (ss->post_clone)
2955 ss->post_clone(ss, child);
2956 }
2957
2958 /* All seems fine. Finish by moving the task into the new cgroup */
2959 ret = cgroup_attach_task(child, tsk);
2960 mutex_unlock(&cgroup_mutex);
2961
2962 out_release:
2963 mutex_unlock(&inode->i_mutex);
2964
2965 mutex_lock(&cgroup_mutex);
2966 put_css_set(cg);
2967 mutex_unlock(&cgroup_mutex);
2968 deactivate_super(parent->root->sb);
2969 return ret;
2970 }
2971
2972 /**
2973 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2974 * @cgrp: the cgroup in question
2975 *
2976 * See if @cgrp is a descendant of the current task's cgroup in
2977 * the appropriate hierarchy.
2978 *
2979 * If we are sending in dummytop, then presumably we are creating
2980 * the top cgroup in the subsystem.
2981 *
2982 * Called only by the ns (nsproxy) cgroup.
2983 */
2984 int cgroup_is_descendant(const struct cgroup *cgrp)
2985 {
2986 int ret;
2987 struct cgroup *target;
2988 int subsys_id;
2989
2990 if (cgrp == dummytop)
2991 return 1;
2992
2993 get_first_subsys(cgrp, NULL, &subsys_id);
2994 target = task_cgroup(current, subsys_id);
2995 while (cgrp != target && cgrp!= cgrp->top_cgroup)
2996 cgrp = cgrp->parent;
2997 ret = (cgrp == target);
2998 return ret;
2999 }
3000
3001 static void check_for_release(struct cgroup *cgrp)
3002 {
3003 /* All of these checks rely on RCU to keep the cgroup
3004 * structure alive */
3005 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3006 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
3007 /* Control Group is currently removeable. If it's not
3008 * already queued for a userspace notification, queue
3009 * it now */
3010 int need_schedule_work = 0;
3011 spin_lock(&release_list_lock);
3012 if (!cgroup_is_removed(cgrp) &&
3013 list_empty(&cgrp->release_list)) {
3014 list_add(&cgrp->release_list, &release_list);
3015 need_schedule_work = 1;
3016 }
3017 spin_unlock(&release_list_lock);
3018 if (need_schedule_work)
3019 schedule_work(&release_agent_work);
3020 }
3021 }
3022
3023 void __css_put(struct cgroup_subsys_state *css)
3024 {
3025 struct cgroup *cgrp = css->cgroup;
3026 rcu_read_lock();
3027 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3028 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3029 check_for_release(cgrp);
3030 }
3031 rcu_read_unlock();
3032 }
3033
3034 /*
3035 * Notify userspace when a cgroup is released, by running the
3036 * configured release agent with the name of the cgroup (path
3037 * relative to the root of cgroup file system) as the argument.
3038 *
3039 * Most likely, this user command will try to rmdir this cgroup.
3040 *
3041 * This races with the possibility that some other task will be
3042 * attached to this cgroup before it is removed, or that some other
3043 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3044 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3045 * unused, and this cgroup will be reprieved from its death sentence,
3046 * to continue to serve a useful existence. Next time it's released,
3047 * we will get notified again, if it still has 'notify_on_release' set.
3048 *
3049 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3050 * means only wait until the task is successfully execve()'d. The
3051 * separate release agent task is forked by call_usermodehelper(),
3052 * then control in this thread returns here, without waiting for the
3053 * release agent task. We don't bother to wait because the caller of
3054 * this routine has no use for the exit status of the release agent
3055 * task, so no sense holding our caller up for that.
3056 */
3057 static void cgroup_release_agent(struct work_struct *work)
3058 {
3059 BUG_ON(work != &release_agent_work);
3060 mutex_lock(&cgroup_mutex);
3061 spin_lock(&release_list_lock);
3062 while (!list_empty(&release_list)) {
3063 char *argv[3], *envp[3];
3064 int i;
3065 char *pathbuf;
3066 struct cgroup *cgrp = list_entry(release_list.next,
3067 struct cgroup,
3068 release_list);
3069 list_del_init(&cgrp->release_list);
3070 spin_unlock(&release_list_lock);
3071 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3072 if (!pathbuf) {
3073 spin_lock(&release_list_lock);
3074 continue;
3075 }
3076
3077 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) {
3078 kfree(pathbuf);
3079 spin_lock(&release_list_lock);
3080 continue;
3081 }
3082
3083 i = 0;
3084 argv[i++] = cgrp->root->release_agent_path;
3085 argv[i++] = (char *)pathbuf;
3086 argv[i] = NULL;
3087
3088 i = 0;
3089 /* minimal command environment */
3090 envp[i++] = "HOME=/";
3091 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3092 envp[i] = NULL;
3093
3094 /* Drop the lock while we invoke the usermode helper,
3095 * since the exec could involve hitting disk and hence
3096 * be a slow process */
3097 mutex_unlock(&cgroup_mutex);
3098 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3099 kfree(pathbuf);
3100 mutex_lock(&cgroup_mutex);
3101 spin_lock(&release_list_lock);
3102 }
3103 spin_unlock(&release_list_lock);
3104 mutex_unlock(&cgroup_mutex);
3105 }
3106
3107 static int __init cgroup_disable(char *str)
3108 {
3109 int i;
3110 char *token;
3111
3112 while ((token = strsep(&str, ",")) != NULL) {
3113 if (!*token)
3114 continue;
3115
3116 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3117 struct cgroup_subsys *ss = subsys[i];
3118
3119 if (!strcmp(token, ss->name)) {
3120 ss->disabled = 1;
3121 printk(KERN_INFO "Disabling %s control group"
3122 " subsystem\n", ss->name);
3123 break;
3124 }
3125 }
3126 }
3127 return 1;
3128 }
3129 __setup("cgroup_disable=", cgroup_disable);