sched/headers: Move task_struct::signal and task_struct::sighand types and accessors...
[linux-block.git] / kernel / cgroup / cgroup-v1.c
CommitLineData
0a268dbd
TH
1#include "cgroup-internal.h"
2
1592c9b2 3#include <linux/ctype.h>
0a268dbd
TH
4#include <linux/kmod.h>
5#include <linux/sort.h>
1592c9b2 6#include <linux/delay.h>
0a268dbd 7#include <linux/mm.h>
c3edc401 8#include <linux/sched/signal.h>
0a268dbd
TH
9#include <linux/slab.h>
10#include <linux/vmalloc.h>
11#include <linux/delayacct.h>
12#include <linux/pid_namespace.h>
13#include <linux/cgroupstats.h>
14
15#include <trace/events/cgroup.h>
16
17/*
18 * pidlists linger the following amount before being destroyed. The goal
19 * is avoiding frequent destruction in the middle of consecutive read calls
20 * Expiring in the middle is a performance problem not a correctness one.
21 * 1 sec should be enough.
22 */
23#define CGROUP_PIDLIST_DESTROY_DELAY HZ
24
25/* Controllers blocked by the commandline in v1 */
26static u16 cgroup_no_v1_mask;
27
28/*
29 * pidlist destructions need to be flushed on cgroup destruction. Use a
30 * separate workqueue as flush domain.
31 */
32static struct workqueue_struct *cgroup_pidlist_destroy_wq;
33
34/*
35 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
36 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
37 */
1592c9b2 38static DEFINE_SPINLOCK(release_agent_path_lock);
0a268dbd 39
d62beb7f 40bool cgroup1_ssid_disabled(int ssid)
0a268dbd
TH
41{
42 return cgroup_no_v1_mask & (1 << ssid);
43}
44
45/**
46 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
47 * @from: attach to all cgroups of a given task
48 * @tsk: the task to be attached
49 */
50int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
51{
52 struct cgroup_root *root;
53 int retval = 0;
54
55 mutex_lock(&cgroup_mutex);
56 percpu_down_write(&cgroup_threadgroup_rwsem);
57 for_each_root(root) {
58 struct cgroup *from_cgrp;
59
60 if (root == &cgrp_dfl_root)
61 continue;
62
63 spin_lock_irq(&css_set_lock);
64 from_cgrp = task_cgroup_from_root(from, root);
65 spin_unlock_irq(&css_set_lock);
66
67 retval = cgroup_attach_task(from_cgrp, tsk, false);
68 if (retval)
69 break;
70 }
71 percpu_up_write(&cgroup_threadgroup_rwsem);
72 mutex_unlock(&cgroup_mutex);
73
74 return retval;
75}
76EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
77
78/**
79 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
80 * @to: cgroup to which the tasks will be moved
81 * @from: cgroup in which the tasks currently reside
82 *
83 * Locking rules between cgroup_post_fork() and the migration path
84 * guarantee that, if a task is forking while being migrated, the new child
85 * is guaranteed to be either visible in the source cgroup after the
86 * parent's migration is complete or put into the target cgroup. No task
87 * can slip out of migration through forking.
88 */
89int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
90{
e595cd70 91 DEFINE_CGROUP_MGCTX(mgctx);
0a268dbd
TH
92 struct cgrp_cset_link *link;
93 struct css_task_iter it;
94 struct task_struct *task;
95 int ret;
96
97 if (cgroup_on_dfl(to))
98 return -EINVAL;
99
100 if (!cgroup_may_migrate_to(to))
101 return -EBUSY;
102
103 mutex_lock(&cgroup_mutex);
104
105 percpu_down_write(&cgroup_threadgroup_rwsem);
106
107 /* all tasks in @from are being moved, all csets are source */
108 spin_lock_irq(&css_set_lock);
109 list_for_each_entry(link, &from->cset_links, cset_link)
e595cd70 110 cgroup_migrate_add_src(link->cset, to, &mgctx);
0a268dbd
TH
111 spin_unlock_irq(&css_set_lock);
112
e595cd70 113 ret = cgroup_migrate_prepare_dst(&mgctx);
0a268dbd
TH
114 if (ret)
115 goto out_err;
116
117 /*
118 * Migrate tasks one-by-one until @from is empty. This fails iff
119 * ->can_attach() fails.
120 */
121 do {
122 css_task_iter_start(&from->self, &it);
123 task = css_task_iter_next(&it);
124 if (task)
125 get_task_struct(task);
126 css_task_iter_end(&it);
127
128 if (task) {
bfc2cf6f 129 ret = cgroup_migrate(task, false, &mgctx);
0a268dbd
TH
130 if (!ret)
131 trace_cgroup_transfer_tasks(to, task, false);
132 put_task_struct(task);
133 }
134 } while (task && !ret);
135out_err:
e595cd70 136 cgroup_migrate_finish(&mgctx);
0a268dbd
TH
137 percpu_up_write(&cgroup_threadgroup_rwsem);
138 mutex_unlock(&cgroup_mutex);
139 return ret;
140}
141
142/*
143 * Stuff for reading the 'tasks'/'procs' files.
144 *
145 * Reading this file can return large amounts of data if a cgroup has
146 * *lots* of attached tasks. So it may need several calls to read(),
147 * but we cannot guarantee that the information we produce is correct
148 * unless we produce it entirely atomically.
149 *
150 */
151
152/* which pidlist file are we talking about? */
153enum cgroup_filetype {
154 CGROUP_FILE_PROCS,
155 CGROUP_FILE_TASKS,
156};
157
158/*
159 * A pidlist is a list of pids that virtually represents the contents of one
160 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
161 * a pair (one each for procs, tasks) for each pid namespace that's relevant
162 * to the cgroup.
163 */
164struct cgroup_pidlist {
165 /*
166 * used to find which pidlist is wanted. doesn't change as long as
167 * this particular list stays in the list.
168 */
169 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
170 /* array of xids */
171 pid_t *list;
172 /* how many elements the above list has */
173 int length;
174 /* each of these stored in a list by its cgroup */
175 struct list_head links;
176 /* pointer to the cgroup we belong to, for list removal purposes */
177 struct cgroup *owner;
178 /* for delayed destruction */
179 struct delayed_work destroy_dwork;
180};
181
182/*
183 * The following two functions "fix" the issue where there are more pids
184 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
185 * TODO: replace with a kernel-wide solution to this problem
186 */
187#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
188static void *pidlist_allocate(int count)
189{
190 if (PIDLIST_TOO_LARGE(count))
191 return vmalloc(count * sizeof(pid_t));
192 else
193 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
194}
195
196static void pidlist_free(void *p)
197{
198 kvfree(p);
199}
200
201/*
202 * Used to destroy all pidlists lingering waiting for destroy timer. None
203 * should be left afterwards.
204 */
d62beb7f 205void cgroup1_pidlist_destroy_all(struct cgroup *cgrp)
0a268dbd
TH
206{
207 struct cgroup_pidlist *l, *tmp_l;
208
209 mutex_lock(&cgrp->pidlist_mutex);
210 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
211 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
212 mutex_unlock(&cgrp->pidlist_mutex);
213
214 flush_workqueue(cgroup_pidlist_destroy_wq);
215 BUG_ON(!list_empty(&cgrp->pidlists));
216}
217
218static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
219{
220 struct delayed_work *dwork = to_delayed_work(work);
221 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
222 destroy_dwork);
223 struct cgroup_pidlist *tofree = NULL;
224
225 mutex_lock(&l->owner->pidlist_mutex);
226
227 /*
228 * Destroy iff we didn't get queued again. The state won't change
229 * as destroy_dwork can only be queued while locked.
230 */
231 if (!delayed_work_pending(dwork)) {
232 list_del(&l->links);
233 pidlist_free(l->list);
234 put_pid_ns(l->key.ns);
235 tofree = l;
236 }
237
238 mutex_unlock(&l->owner->pidlist_mutex);
239 kfree(tofree);
240}
241
242/*
243 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
244 * Returns the number of unique elements.
245 */
246static int pidlist_uniq(pid_t *list, int length)
247{
248 int src, dest = 1;
249
250 /*
251 * we presume the 0th element is unique, so i starts at 1. trivial
252 * edge cases first; no work needs to be done for either
253 */
254 if (length == 0 || length == 1)
255 return length;
256 /* src and dest walk down the list; dest counts unique elements */
257 for (src = 1; src < length; src++) {
258 /* find next unique element */
259 while (list[src] == list[src-1]) {
260 src++;
261 if (src == length)
262 goto after;
263 }
264 /* dest always points to where the next unique element goes */
265 list[dest] = list[src];
266 dest++;
267 }
268after:
269 return dest;
270}
271
272/*
273 * The two pid files - task and cgroup.procs - guaranteed that the result
274 * is sorted, which forced this whole pidlist fiasco. As pid order is
275 * different per namespace, each namespace needs differently sorted list,
276 * making it impossible to use, for example, single rbtree of member tasks
277 * sorted by task pointer. As pidlists can be fairly large, allocating one
278 * per open file is dangerous, so cgroup had to implement shared pool of
279 * pidlists keyed by cgroup and namespace.
280 */
281static int cmppid(const void *a, const void *b)
282{
283 return *(pid_t *)a - *(pid_t *)b;
284}
285
286static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
287 enum cgroup_filetype type)
288{
289 struct cgroup_pidlist *l;
290 /* don't need task_nsproxy() if we're looking at ourself */
291 struct pid_namespace *ns = task_active_pid_ns(current);
292
293 lockdep_assert_held(&cgrp->pidlist_mutex);
294
295 list_for_each_entry(l, &cgrp->pidlists, links)
296 if (l->key.type == type && l->key.ns == ns)
297 return l;
298 return NULL;
299}
300
301/*
302 * find the appropriate pidlist for our purpose (given procs vs tasks)
303 * returns with the lock on that pidlist already held, and takes care
304 * of the use count, or returns NULL with no locks held if we're out of
305 * memory.
306 */
307static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
308 enum cgroup_filetype type)
309{
310 struct cgroup_pidlist *l;
311
312 lockdep_assert_held(&cgrp->pidlist_mutex);
313
314 l = cgroup_pidlist_find(cgrp, type);
315 if (l)
316 return l;
317
318 /* entry not found; create a new one */
319 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
320 if (!l)
321 return l;
322
323 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
324 l->key.type = type;
325 /* don't need task_nsproxy() if we're looking at ourself */
326 l->key.ns = get_pid_ns(task_active_pid_ns(current));
327 l->owner = cgrp;
328 list_add(&l->links, &cgrp->pidlists);
329 return l;
330}
331
332/**
333 * cgroup_task_count - count the number of tasks in a cgroup.
334 * @cgrp: the cgroup in question
335 *
336 * Return the number of tasks in the cgroup. The returned number can be
337 * higher than the actual number of tasks due to css_set references from
338 * namespace roots and temporary usages.
339 */
340static int cgroup_task_count(const struct cgroup *cgrp)
341{
342 int count = 0;
343 struct cgrp_cset_link *link;
344
345 spin_lock_irq(&css_set_lock);
346 list_for_each_entry(link, &cgrp->cset_links, cset_link)
347 count += atomic_read(&link->cset->refcount);
348 spin_unlock_irq(&css_set_lock);
349 return count;
350}
351
352/*
353 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
354 */
355static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
356 struct cgroup_pidlist **lp)
357{
358 pid_t *array;
359 int length;
360 int pid, n = 0; /* used for populating the array */
361 struct css_task_iter it;
362 struct task_struct *tsk;
363 struct cgroup_pidlist *l;
364
365 lockdep_assert_held(&cgrp->pidlist_mutex);
366
367 /*
368 * If cgroup gets more users after we read count, we won't have
369 * enough space - tough. This race is indistinguishable to the
370 * caller from the case that the additional cgroup users didn't
371 * show up until sometime later on.
372 */
373 length = cgroup_task_count(cgrp);
374 array = pidlist_allocate(length);
375 if (!array)
376 return -ENOMEM;
377 /* now, populate the array */
378 css_task_iter_start(&cgrp->self, &it);
379 while ((tsk = css_task_iter_next(&it))) {
380 if (unlikely(n == length))
381 break;
382 /* get tgid or pid for procs or tasks file respectively */
383 if (type == CGROUP_FILE_PROCS)
384 pid = task_tgid_vnr(tsk);
385 else
386 pid = task_pid_vnr(tsk);
387 if (pid > 0) /* make sure to only use valid results */
388 array[n++] = pid;
389 }
390 css_task_iter_end(&it);
391 length = n;
392 /* now sort & (if procs) strip out duplicates */
393 sort(array, length, sizeof(pid_t), cmppid, NULL);
394 if (type == CGROUP_FILE_PROCS)
395 length = pidlist_uniq(array, length);
396
397 l = cgroup_pidlist_find_create(cgrp, type);
398 if (!l) {
399 pidlist_free(array);
400 return -ENOMEM;
401 }
402
403 /* store array, freeing old if necessary */
404 pidlist_free(l->list);
405 l->list = array;
406 l->length = length;
407 *lp = l;
408 return 0;
409}
410
411/*
412 * seq_file methods for the tasks/procs files. The seq_file position is the
413 * next pid to display; the seq_file iterator is a pointer to the pid
414 * in the cgroup->l->list array.
415 */
416
417static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
418{
419 /*
420 * Initially we receive a position value that corresponds to
421 * one more than the last pid shown (or 0 on the first call or
422 * after a seek to the start). Use a binary-search to find the
423 * next pid to display, if any
424 */
425 struct kernfs_open_file *of = s->private;
426 struct cgroup *cgrp = seq_css(s)->cgroup;
427 struct cgroup_pidlist *l;
428 enum cgroup_filetype type = seq_cft(s)->private;
429 int index = 0, pid = *pos;
430 int *iter, ret;
431
432 mutex_lock(&cgrp->pidlist_mutex);
433
434 /*
435 * !NULL @of->priv indicates that this isn't the first start()
436 * after open. If the matching pidlist is around, we can use that.
437 * Look for it. Note that @of->priv can't be used directly. It
438 * could already have been destroyed.
439 */
440 if (of->priv)
441 of->priv = cgroup_pidlist_find(cgrp, type);
442
443 /*
444 * Either this is the first start() after open or the matching
445 * pidlist has been destroyed inbetween. Create a new one.
446 */
447 if (!of->priv) {
448 ret = pidlist_array_load(cgrp, type,
449 (struct cgroup_pidlist **)&of->priv);
450 if (ret)
451 return ERR_PTR(ret);
452 }
453 l = of->priv;
454
455 if (pid) {
456 int end = l->length;
457
458 while (index < end) {
459 int mid = (index + end) / 2;
460 if (l->list[mid] == pid) {
461 index = mid;
462 break;
463 } else if (l->list[mid] <= pid)
464 index = mid + 1;
465 else
466 end = mid;
467 }
468 }
469 /* If we're off the end of the array, we're done */
470 if (index >= l->length)
471 return NULL;
472 /* Update the abstract position to be the actual pid that we found */
473 iter = l->list + index;
474 *pos = *iter;
475 return iter;
476}
477
478static void cgroup_pidlist_stop(struct seq_file *s, void *v)
479{
480 struct kernfs_open_file *of = s->private;
481 struct cgroup_pidlist *l = of->priv;
482
483 if (l)
484 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
485 CGROUP_PIDLIST_DESTROY_DELAY);
486 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
487}
488
489static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
490{
491 struct kernfs_open_file *of = s->private;
492 struct cgroup_pidlist *l = of->priv;
493 pid_t *p = v;
494 pid_t *end = l->list + l->length;
495 /*
496 * Advance to the next pid in the array. If this goes off the
497 * end, we're done
498 */
499 p++;
500 if (p >= end) {
501 return NULL;
502 } else {
503 *pos = *p;
504 return p;
505 }
506}
507
508static int cgroup_pidlist_show(struct seq_file *s, void *v)
509{
510 seq_printf(s, "%d\n", *(int *)v);
511
512 return 0;
513}
514
515static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
516 char *buf, size_t nbytes, loff_t off)
517{
518 return __cgroup_procs_write(of, buf, nbytes, off, false);
519}
520
521static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
522 char *buf, size_t nbytes, loff_t off)
523{
524 struct cgroup *cgrp;
525
526 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
527
528 cgrp = cgroup_kn_lock_live(of->kn, false);
529 if (!cgrp)
530 return -ENODEV;
531 spin_lock(&release_agent_path_lock);
532 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
533 sizeof(cgrp->root->release_agent_path));
534 spin_unlock(&release_agent_path_lock);
535 cgroup_kn_unlock(of->kn);
536 return nbytes;
537}
538
539static int cgroup_release_agent_show(struct seq_file *seq, void *v)
540{
541 struct cgroup *cgrp = seq_css(seq)->cgroup;
542
543 spin_lock(&release_agent_path_lock);
544 seq_puts(seq, cgrp->root->release_agent_path);
545 spin_unlock(&release_agent_path_lock);
546 seq_putc(seq, '\n');
547 return 0;
548}
549
550static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
551{
552 seq_puts(seq, "0\n");
553 return 0;
554}
555
556static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
557 struct cftype *cft)
558{
559 return notify_on_release(css->cgroup);
560}
561
562static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
563 struct cftype *cft, u64 val)
564{
565 if (val)
566 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
567 else
568 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
569 return 0;
570}
571
572static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
573 struct cftype *cft)
574{
575 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
576}
577
578static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
579 struct cftype *cft, u64 val)
580{
581 if (val)
582 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
583 else
584 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
585 return 0;
586}
587
588/* cgroup core interface files for the legacy hierarchies */
d62beb7f 589struct cftype cgroup1_base_files[] = {
0a268dbd
TH
590 {
591 .name = "cgroup.procs",
592 .seq_start = cgroup_pidlist_start,
593 .seq_next = cgroup_pidlist_next,
594 .seq_stop = cgroup_pidlist_stop,
595 .seq_show = cgroup_pidlist_show,
596 .private = CGROUP_FILE_PROCS,
597 .write = cgroup_procs_write,
598 },
599 {
600 .name = "cgroup.clone_children",
601 .read_u64 = cgroup_clone_children_read,
602 .write_u64 = cgroup_clone_children_write,
603 },
604 {
605 .name = "cgroup.sane_behavior",
606 .flags = CFTYPE_ONLY_ON_ROOT,
607 .seq_show = cgroup_sane_behavior_show,
608 },
609 {
610 .name = "tasks",
611 .seq_start = cgroup_pidlist_start,
612 .seq_next = cgroup_pidlist_next,
613 .seq_stop = cgroup_pidlist_stop,
614 .seq_show = cgroup_pidlist_show,
615 .private = CGROUP_FILE_TASKS,
616 .write = cgroup_tasks_write,
617 },
618 {
619 .name = "notify_on_release",
620 .read_u64 = cgroup_read_notify_on_release,
621 .write_u64 = cgroup_write_notify_on_release,
622 },
623 {
624 .name = "release_agent",
625 .flags = CFTYPE_ONLY_ON_ROOT,
626 .seq_show = cgroup_release_agent_show,
627 .write = cgroup_release_agent_write,
628 .max_write_len = PATH_MAX - 1,
629 },
630 { } /* terminate */
631};
632
633/* Display information about each subsystem and each hierarchy */
634static int proc_cgroupstats_show(struct seq_file *m, void *v)
635{
636 struct cgroup_subsys *ss;
637 int i;
638
639 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
640 /*
641 * ideally we don't want subsystems moving around while we do this.
642 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
643 * subsys/hierarchy state.
644 */
645 mutex_lock(&cgroup_mutex);
646
647 for_each_subsys(ss, i)
648 seq_printf(m, "%s\t%d\t%d\t%d\n",
649 ss->legacy_name, ss->root->hierarchy_id,
650 atomic_read(&ss->root->nr_cgrps),
651 cgroup_ssid_enabled(i));
652
653 mutex_unlock(&cgroup_mutex);
654 return 0;
655}
656
657static int cgroupstats_open(struct inode *inode, struct file *file)
658{
659 return single_open(file, proc_cgroupstats_show, NULL);
660}
661
662const struct file_operations proc_cgroupstats_operations = {
663 .open = cgroupstats_open,
664 .read = seq_read,
665 .llseek = seq_lseek,
666 .release = single_release,
667};
668
669/**
670 * cgroupstats_build - build and fill cgroupstats
671 * @stats: cgroupstats to fill information into
672 * @dentry: A dentry entry belonging to the cgroup for which stats have
673 * been requested.
674 *
675 * Build and fill cgroupstats so that taskstats can export it to user
676 * space.
677 */
678int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
679{
680 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
681 struct cgroup *cgrp;
682 struct css_task_iter it;
683 struct task_struct *tsk;
684
685 /* it should be kernfs_node belonging to cgroupfs and is a directory */
686 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
687 kernfs_type(kn) != KERNFS_DIR)
688 return -EINVAL;
689
690 mutex_lock(&cgroup_mutex);
691
692 /*
693 * We aren't being called from kernfs and there's no guarantee on
694 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
695 * @kn->priv is RCU safe. Let's do the RCU dancing.
696 */
697 rcu_read_lock();
e0aed7c7 698 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
0a268dbd
TH
699 if (!cgrp || cgroup_is_dead(cgrp)) {
700 rcu_read_unlock();
701 mutex_unlock(&cgroup_mutex);
702 return -ENOENT;
703 }
704 rcu_read_unlock();
705
706 css_task_iter_start(&cgrp->self, &it);
707 while ((tsk = css_task_iter_next(&it))) {
708 switch (tsk->state) {
709 case TASK_RUNNING:
710 stats->nr_running++;
711 break;
712 case TASK_INTERRUPTIBLE:
713 stats->nr_sleeping++;
714 break;
715 case TASK_UNINTERRUPTIBLE:
716 stats->nr_uninterruptible++;
717 break;
718 case TASK_STOPPED:
719 stats->nr_stopped++;
720 break;
721 default:
722 if (delayacct_is_task_waiting_on_io(tsk))
723 stats->nr_io_wait++;
724 break;
725 }
726 }
727 css_task_iter_end(&it);
728
729 mutex_unlock(&cgroup_mutex);
730 return 0;
731}
732
d62beb7f 733void cgroup1_check_for_release(struct cgroup *cgrp)
0a268dbd
TH
734{
735 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
736 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
737 schedule_work(&cgrp->release_agent_work);
738}
739
740/*
741 * Notify userspace when a cgroup is released, by running the
742 * configured release agent with the name of the cgroup (path
743 * relative to the root of cgroup file system) as the argument.
744 *
745 * Most likely, this user command will try to rmdir this cgroup.
746 *
747 * This races with the possibility that some other task will be
748 * attached to this cgroup before it is removed, or that some other
749 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
750 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
751 * unused, and this cgroup will be reprieved from its death sentence,
752 * to continue to serve a useful existence. Next time it's released,
753 * we will get notified again, if it still has 'notify_on_release' set.
754 *
755 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
756 * means only wait until the task is successfully execve()'d. The
757 * separate release agent task is forked by call_usermodehelper(),
758 * then control in this thread returns here, without waiting for the
759 * release agent task. We don't bother to wait because the caller of
760 * this routine has no use for the exit status of the release agent
761 * task, so no sense holding our caller up for that.
762 */
d62beb7f 763void cgroup1_release_agent(struct work_struct *work)
0a268dbd
TH
764{
765 struct cgroup *cgrp =
766 container_of(work, struct cgroup, release_agent_work);
767 char *pathbuf = NULL, *agentbuf = NULL;
768 char *argv[3], *envp[3];
769 int ret;
770
771 mutex_lock(&cgroup_mutex);
772
773 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
774 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
775 if (!pathbuf || !agentbuf)
776 goto out;
777
778 spin_lock_irq(&css_set_lock);
779 ret = cgroup_path_ns_locked(cgrp, pathbuf, PATH_MAX, &init_cgroup_ns);
780 spin_unlock_irq(&css_set_lock);
781 if (ret < 0 || ret >= PATH_MAX)
782 goto out;
783
784 argv[0] = agentbuf;
785 argv[1] = pathbuf;
786 argv[2] = NULL;
787
788 /* minimal command environment */
789 envp[0] = "HOME=/";
790 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
791 envp[2] = NULL;
792
793 mutex_unlock(&cgroup_mutex);
794 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
795 goto out_free;
796out:
797 mutex_unlock(&cgroup_mutex);
798out_free:
799 kfree(agentbuf);
800 kfree(pathbuf);
801}
802
803/*
804 * cgroup_rename - Only allow simple rename of directories in place.
805 */
1592c9b2
TH
806static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
807 const char *new_name_str)
0a268dbd
TH
808{
809 struct cgroup *cgrp = kn->priv;
810 int ret;
811
812 if (kernfs_type(kn) != KERNFS_DIR)
813 return -ENOTDIR;
814 if (kn->parent != new_parent)
815 return -EIO;
816
0a268dbd
TH
817 /*
818 * We're gonna grab cgroup_mutex which nests outside kernfs
819 * active_ref. kernfs_rename() doesn't require active_ref
820 * protection. Break them before grabbing cgroup_mutex.
821 */
822 kernfs_break_active_protection(new_parent);
823 kernfs_break_active_protection(kn);
824
825 mutex_lock(&cgroup_mutex);
826
827 ret = kernfs_rename(kn, new_parent, new_name_str);
828 if (!ret)
829 trace_cgroup_rename(cgrp);
830
831 mutex_unlock(&cgroup_mutex);
832
833 kernfs_unbreak_active_protection(kn);
834 kernfs_unbreak_active_protection(new_parent);
835 return ret;
836}
837
1592c9b2
TH
838static int cgroup1_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
839{
840 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
841 struct cgroup_subsys *ss;
842 int ssid;
843
844 for_each_subsys(ss, ssid)
845 if (root->subsys_mask & (1 << ssid))
846 seq_show_option(seq, ss->legacy_name, NULL);
847 if (root->flags & CGRP_ROOT_NOPREFIX)
848 seq_puts(seq, ",noprefix");
849 if (root->flags & CGRP_ROOT_XATTR)
850 seq_puts(seq, ",xattr");
851
852 spin_lock(&release_agent_path_lock);
853 if (strlen(root->release_agent_path))
854 seq_show_option(seq, "release_agent",
855 root->release_agent_path);
856 spin_unlock(&release_agent_path_lock);
857
858 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
859 seq_puts(seq, ",clone_children");
860 if (strlen(root->name))
861 seq_show_option(seq, "name", root->name);
862 return 0;
863}
864
865static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
866{
867 char *token, *o = data;
868 bool all_ss = false, one_ss = false;
869 u16 mask = U16_MAX;
870 struct cgroup_subsys *ss;
871 int nr_opts = 0;
872 int i;
873
874#ifdef CONFIG_CPUSETS
875 mask = ~((u16)1 << cpuset_cgrp_id);
876#endif
877
878 memset(opts, 0, sizeof(*opts));
879
880 while ((token = strsep(&o, ",")) != NULL) {
881 nr_opts++;
882
883 if (!*token)
884 return -EINVAL;
885 if (!strcmp(token, "none")) {
886 /* Explicitly have no subsystems */
887 opts->none = true;
888 continue;
889 }
890 if (!strcmp(token, "all")) {
891 /* Mutually exclusive option 'all' + subsystem name */
892 if (one_ss)
893 return -EINVAL;
894 all_ss = true;
895 continue;
896 }
897 if (!strcmp(token, "noprefix")) {
898 opts->flags |= CGRP_ROOT_NOPREFIX;
899 continue;
900 }
901 if (!strcmp(token, "clone_children")) {
902 opts->cpuset_clone_children = true;
903 continue;
904 }
905 if (!strcmp(token, "xattr")) {
906 opts->flags |= CGRP_ROOT_XATTR;
907 continue;
908 }
909 if (!strncmp(token, "release_agent=", 14)) {
910 /* Specifying two release agents is forbidden */
911 if (opts->release_agent)
912 return -EINVAL;
913 opts->release_agent =
914 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
915 if (!opts->release_agent)
916 return -ENOMEM;
917 continue;
918 }
919 if (!strncmp(token, "name=", 5)) {
920 const char *name = token + 5;
921 /* Can't specify an empty name */
922 if (!strlen(name))
923 return -EINVAL;
924 /* Must match [\w.-]+ */
925 for (i = 0; i < strlen(name); i++) {
926 char c = name[i];
927 if (isalnum(c))
928 continue;
929 if ((c == '.') || (c == '-') || (c == '_'))
930 continue;
931 return -EINVAL;
932 }
933 /* Specifying two names is forbidden */
934 if (opts->name)
935 return -EINVAL;
936 opts->name = kstrndup(name,
937 MAX_CGROUP_ROOT_NAMELEN - 1,
938 GFP_KERNEL);
939 if (!opts->name)
940 return -ENOMEM;
941
942 continue;
943 }
944
945 for_each_subsys(ss, i) {
946 if (strcmp(token, ss->legacy_name))
947 continue;
948 if (!cgroup_ssid_enabled(i))
949 continue;
d62beb7f 950 if (cgroup1_ssid_disabled(i))
1592c9b2
TH
951 continue;
952
953 /* Mutually exclusive option 'all' + subsystem name */
954 if (all_ss)
955 return -EINVAL;
956 opts->subsys_mask |= (1 << i);
957 one_ss = true;
958
959 break;
960 }
961 if (i == CGROUP_SUBSYS_COUNT)
962 return -ENOENT;
963 }
964
965 /*
966 * If the 'all' option was specified select all the subsystems,
967 * otherwise if 'none', 'name=' and a subsystem name options were
968 * not specified, let's default to 'all'
969 */
970 if (all_ss || (!one_ss && !opts->none && !opts->name))
971 for_each_subsys(ss, i)
d62beb7f 972 if (cgroup_ssid_enabled(i) && !cgroup1_ssid_disabled(i))
1592c9b2
TH
973 opts->subsys_mask |= (1 << i);
974
975 /*
976 * We either have to specify by name or by subsystems. (So all
977 * empty hierarchies must have a name).
978 */
979 if (!opts->subsys_mask && !opts->name)
980 return -EINVAL;
981
982 /*
983 * Option noprefix was introduced just for backward compatibility
984 * with the old cpuset, so we allow noprefix only if mounting just
985 * the cpuset subsystem.
986 */
987 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
988 return -EINVAL;
989
990 /* Can't specify "none" and some subsystems */
991 if (opts->subsys_mask && opts->none)
992 return -EINVAL;
993
994 return 0;
995}
996
997static int cgroup1_remount(struct kernfs_root *kf_root, int *flags, char *data)
998{
999 int ret = 0;
1000 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1001 struct cgroup_sb_opts opts;
1002 u16 added_mask, removed_mask;
1003
1004 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1005
1006 /* See what subsystems are wanted */
1007 ret = parse_cgroupfs_options(data, &opts);
1008 if (ret)
1009 goto out_unlock;
1010
1011 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
1012 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
1013 task_tgid_nr(current), current->comm);
1014
1015 added_mask = opts.subsys_mask & ~root->subsys_mask;
1016 removed_mask = root->subsys_mask & ~opts.subsys_mask;
1017
1018 /* Don't allow flags or name to change at remount */
1019 if ((opts.flags ^ root->flags) ||
1020 (opts.name && strcmp(opts.name, root->name))) {
1021 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
1022 opts.flags, opts.name ?: "", root->flags, root->name);
1023 ret = -EINVAL;
1024 goto out_unlock;
1025 }
1026
1027 /* remounting is not allowed for populated hierarchies */
1028 if (!list_empty(&root->cgrp.self.children)) {
1029 ret = -EBUSY;
1030 goto out_unlock;
1031 }
1032
1033 ret = rebind_subsystems(root, added_mask);
1034 if (ret)
1035 goto out_unlock;
1036
1037 WARN_ON(rebind_subsystems(&cgrp_dfl_root, removed_mask));
1038
1039 if (opts.release_agent) {
1040 spin_lock(&release_agent_path_lock);
1041 strcpy(root->release_agent_path, opts.release_agent);
1042 spin_unlock(&release_agent_path_lock);
1043 }
1044
1045 trace_cgroup_remount(root);
1046
1047 out_unlock:
1048 kfree(opts.release_agent);
1049 kfree(opts.name);
1050 mutex_unlock(&cgroup_mutex);
1051 return ret;
1052}
1053
1054struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
1055 .rename = cgroup1_rename,
1056 .show_options = cgroup1_show_options,
1057 .remount_fs = cgroup1_remount,
1058 .mkdir = cgroup_mkdir,
1059 .rmdir = cgroup_rmdir,
1060 .show_path = cgroup_show_path,
1061};
1062
1063struct dentry *cgroup1_mount(struct file_system_type *fs_type, int flags,
1064 void *data, unsigned long magic,
1065 struct cgroup_namespace *ns)
1066{
1067 struct super_block *pinned_sb = NULL;
1068 struct cgroup_sb_opts opts;
1069 struct cgroup_root *root;
1070 struct cgroup_subsys *ss;
1071 struct dentry *dentry;
1072 int i, ret;
1073
1074 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1075
1076 /* First find the desired set of subsystems */
1077 ret = parse_cgroupfs_options(data, &opts);
1078 if (ret)
1079 goto out_unlock;
1080
1081 /*
1082 * Destruction of cgroup root is asynchronous, so subsystems may
1083 * still be dying after the previous unmount. Let's drain the
1084 * dying subsystems. We just need to ensure that the ones
1085 * unmounted previously finish dying and don't care about new ones
1086 * starting. Testing ref liveliness is good enough.
1087 */
1088 for_each_subsys(ss, i) {
1089 if (!(opts.subsys_mask & (1 << i)) ||
1090 ss->root == &cgrp_dfl_root)
1091 continue;
1092
1093 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1094 mutex_unlock(&cgroup_mutex);
1095 msleep(10);
1096 ret = restart_syscall();
1097 goto out_free;
1098 }
1099 cgroup_put(&ss->root->cgrp);
1100 }
1101
1102 for_each_root(root) {
1103 bool name_match = false;
1104
1105 if (root == &cgrp_dfl_root)
1106 continue;
1107
1108 /*
1109 * If we asked for a name then it must match. Also, if
1110 * name matches but sybsys_mask doesn't, we should fail.
1111 * Remember whether name matched.
1112 */
1113 if (opts.name) {
1114 if (strcmp(opts.name, root->name))
1115 continue;
1116 name_match = true;
1117 }
1118
1119 /*
1120 * If we asked for subsystems (or explicitly for no
1121 * subsystems) then they must match.
1122 */
1123 if ((opts.subsys_mask || opts.none) &&
1124 (opts.subsys_mask != root->subsys_mask)) {
1125 if (!name_match)
1126 continue;
1127 ret = -EBUSY;
1128 goto out_unlock;
1129 }
1130
1131 if (root->flags ^ opts.flags)
1132 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
1133
1134 /*
1135 * We want to reuse @root whose lifetime is governed by its
1136 * ->cgrp. Let's check whether @root is alive and keep it
1137 * that way. As cgroup_kill_sb() can happen anytime, we
1138 * want to block it by pinning the sb so that @root doesn't
1139 * get killed before mount is complete.
1140 *
1141 * With the sb pinned, tryget_live can reliably indicate
1142 * whether @root can be reused. If it's being killed,
1143 * drain it. We can use wait_queue for the wait but this
1144 * path is super cold. Let's just sleep a bit and retry.
1145 */
1146 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1147 if (IS_ERR(pinned_sb) ||
1148 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
1149 mutex_unlock(&cgroup_mutex);
1150 if (!IS_ERR_OR_NULL(pinned_sb))
1151 deactivate_super(pinned_sb);
1152 msleep(10);
1153 ret = restart_syscall();
1154 goto out_free;
1155 }
1156
1157 ret = 0;
1158 goto out_unlock;
1159 }
1160
1161 /*
1162 * No such thing, create a new one. name= matching without subsys
1163 * specification is allowed for already existing hierarchies but we
1164 * can't create new one without subsys specification.
1165 */
1166 if (!opts.subsys_mask && !opts.none) {
1167 ret = -EINVAL;
1168 goto out_unlock;
1169 }
1170
1171 /* Hierarchies may only be created in the initial cgroup namespace. */
1172 if (ns != &init_cgroup_ns) {
1173 ret = -EPERM;
1174 goto out_unlock;
1175 }
1176
1177 root = kzalloc(sizeof(*root), GFP_KERNEL);
1178 if (!root) {
1179 ret = -ENOMEM;
1180 goto out_unlock;
1181 }
1182
1183 init_cgroup_root(root, &opts);
1184
1185 ret = cgroup_setup_root(root, opts.subsys_mask);
1186 if (ret)
1187 cgroup_free_root(root);
1188
1189out_unlock:
1190 mutex_unlock(&cgroup_mutex);
1191out_free:
1192 kfree(opts.release_agent);
1193 kfree(opts.name);
1194
1195 if (ret)
1196 return ERR_PTR(ret);
1197
1198 dentry = cgroup_do_mount(&cgroup_fs_type, flags, root,
1199 CGROUP_SUPER_MAGIC, ns);
1200
1201 /*
1202 * If @pinned_sb, we're reusing an existing root and holding an
1203 * extra ref on its sb. Mount is complete. Put the extra ref.
1204 */
1205 if (pinned_sb)
1206 deactivate_super(pinned_sb);
1207
1208 return dentry;
1209}
1210
0a268dbd
TH
1211static int __init cgroup1_wq_init(void)
1212{
1213 /*
1214 * Used to destroy pidlists and separate to serve as flush domain.
1215 * Cap @max_active to 1 too.
1216 */
1217 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
1218 0, 1);
1219 BUG_ON(!cgroup_pidlist_destroy_wq);
1220 return 0;
1221}
1222core_initcall(cgroup1_wq_init);
1223
1224static int __init cgroup_no_v1(char *str)
1225{
1226 struct cgroup_subsys *ss;
1227 char *token;
1228 int i;
1229
1230 while ((token = strsep(&str, ",")) != NULL) {
1231 if (!*token)
1232 continue;
1233
1234 if (!strcmp(token, "all")) {
1235 cgroup_no_v1_mask = U16_MAX;
1236 break;
1237 }
1238
1239 for_each_subsys(ss, i) {
1240 if (strcmp(token, ss->name) &&
1241 strcmp(token, ss->legacy_name))
1242 continue;
1243
1244 cgroup_no_v1_mask |= 1 << i;
1245 }
1246 }
1247 return 1;
1248}
1249__setup("cgroup_no_v1=", cgroup_no_v1);
1250
1251
1252#ifdef CONFIG_CGROUP_DEBUG
1253static struct cgroup_subsys_state *
1254debug_css_alloc(struct cgroup_subsys_state *parent_css)
1255{
1256 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
1257
1258 if (!css)
1259 return ERR_PTR(-ENOMEM);
1260
1261 return css;
1262}
1263
1264static void debug_css_free(struct cgroup_subsys_state *css)
1265{
1266 kfree(css);
1267}
1268
1269static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
1270 struct cftype *cft)
1271{
1272 return cgroup_task_count(css->cgroup);
1273}
1274
1275static u64 current_css_set_read(struct cgroup_subsys_state *css,
1276 struct cftype *cft)
1277{
1278 return (u64)(unsigned long)current->cgroups;
1279}
1280
1281static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
1282 struct cftype *cft)
1283{
1284 u64 count;
1285
1286 rcu_read_lock();
1287 count = atomic_read(&task_css_set(current)->refcount);
1288 rcu_read_unlock();
1289 return count;
1290}
1291
1292static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
1293{
1294 struct cgrp_cset_link *link;
1295 struct css_set *cset;
1296 char *name_buf;
1297
1298 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
1299 if (!name_buf)
1300 return -ENOMEM;
1301
1302 spin_lock_irq(&css_set_lock);
1303 rcu_read_lock();
1304 cset = rcu_dereference(current->cgroups);
1305 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1306 struct cgroup *c = link->cgrp;
1307
1308 cgroup_name(c, name_buf, NAME_MAX + 1);
1309 seq_printf(seq, "Root %d group %s\n",
1310 c->root->hierarchy_id, name_buf);
1311 }
1312 rcu_read_unlock();
1313 spin_unlock_irq(&css_set_lock);
1314 kfree(name_buf);
1315 return 0;
1316}
1317
1318#define MAX_TASKS_SHOWN_PER_CSS 25
1319static int cgroup_css_links_read(struct seq_file *seq, void *v)
1320{
1321 struct cgroup_subsys_state *css = seq_css(seq);
1322 struct cgrp_cset_link *link;
1323
1324 spin_lock_irq(&css_set_lock);
1325 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
1326 struct css_set *cset = link->cset;
1327 struct task_struct *task;
1328 int count = 0;
1329
1330 seq_printf(seq, "css_set %p\n", cset);
1331
1332 list_for_each_entry(task, &cset->tasks, cg_list) {
1333 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
1334 goto overflow;
1335 seq_printf(seq, " task %d\n", task_pid_vnr(task));
1336 }
1337
1338 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
1339 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
1340 goto overflow;
1341 seq_printf(seq, " task %d\n", task_pid_vnr(task));
1342 }
1343 continue;
1344 overflow:
1345 seq_puts(seq, " ...\n");
1346 }
1347 spin_unlock_irq(&css_set_lock);
1348 return 0;
1349}
1350
1351static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
1352{
1353 return (!cgroup_is_populated(css->cgroup) &&
1354 !css_has_online_children(&css->cgroup->self));
1355}
1356
1357static struct cftype debug_files[] = {
1358 {
1359 .name = "taskcount",
1360 .read_u64 = debug_taskcount_read,
1361 },
1362
1363 {
1364 .name = "current_css_set",
1365 .read_u64 = current_css_set_read,
1366 },
1367
1368 {
1369 .name = "current_css_set_refcount",
1370 .read_u64 = current_css_set_refcount_read,
1371 },
1372
1373 {
1374 .name = "current_css_set_cg_links",
1375 .seq_show = current_css_set_cg_links_read,
1376 },
1377
1378 {
1379 .name = "cgroup_css_links",
1380 .seq_show = cgroup_css_links_read,
1381 },
1382
1383 {
1384 .name = "releasable",
1385 .read_u64 = releasable_read,
1386 },
1387
1388 { } /* terminate */
1389};
1390
1391struct cgroup_subsys debug_cgrp_subsys = {
1392 .css_alloc = debug_css_alloc,
1393 .css_free = debug_css_free,
1394 .legacy_cftypes = debug_files,
1395};
1396#endif /* CONFIG_CGROUP_DEBUG */