1 #include "cgroup-internal.h"
3 #include <linux/ctype.h>
4 #include <linux/kmod.h>
5 #include <linux/sort.h>
6 #include <linux/delay.h>
8 #include <linux/sched/signal.h>
9 #include <linux/sched/task.h>
10 #include <linux/magic.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
13 #include <linux/delayacct.h>
14 #include <linux/pid_namespace.h>
15 #include <linux/cgroupstats.h>
17 #include <trace/events/cgroup.h>
20 * pidlists linger the following amount before being destroyed. The goal
21 * is avoiding frequent destruction in the middle of consecutive read calls
22 * Expiring in the middle is a performance problem not a correctness one.
23 * 1 sec should be enough.
25 #define CGROUP_PIDLIST_DESTROY_DELAY HZ
27 /* Controllers blocked by the commandline in v1 */
28 static u16 cgroup_no_v1_mask
;
31 * pidlist destructions need to be flushed on cgroup destruction. Use a
32 * separate workqueue as flush domain.
34 static struct workqueue_struct
*cgroup_pidlist_destroy_wq
;
37 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
38 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
40 static DEFINE_SPINLOCK(release_agent_path_lock
);
42 bool cgroup1_ssid_disabled(int ssid
)
44 return cgroup_no_v1_mask
& (1 << ssid
);
48 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
49 * @from: attach to all cgroups of a given task
50 * @tsk: the task to be attached
52 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
54 struct cgroup_root
*root
;
57 mutex_lock(&cgroup_mutex
);
58 percpu_down_write(&cgroup_threadgroup_rwsem
);
60 struct cgroup
*from_cgrp
;
62 if (root
== &cgrp_dfl_root
)
65 spin_lock_irq(&css_set_lock
);
66 from_cgrp
= task_cgroup_from_root(from
, root
);
67 spin_unlock_irq(&css_set_lock
);
69 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
73 percpu_up_write(&cgroup_threadgroup_rwsem
);
74 mutex_unlock(&cgroup_mutex
);
78 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
81 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
82 * @to: cgroup to which the tasks will be moved
83 * @from: cgroup in which the tasks currently reside
85 * Locking rules between cgroup_post_fork() and the migration path
86 * guarantee that, if a task is forking while being migrated, the new child
87 * is guaranteed to be either visible in the source cgroup after the
88 * parent's migration is complete or put into the target cgroup. No task
89 * can slip out of migration through forking.
91 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
93 DEFINE_CGROUP_MGCTX(mgctx
);
94 struct cgrp_cset_link
*link
;
95 struct css_task_iter it
;
96 struct task_struct
*task
;
99 if (cgroup_on_dfl(to
))
102 ret
= cgroup_migrate_vet_dst(to
);
106 mutex_lock(&cgroup_mutex
);
108 percpu_down_write(&cgroup_threadgroup_rwsem
);
110 /* all tasks in @from are being moved, all csets are source */
111 spin_lock_irq(&css_set_lock
);
112 list_for_each_entry(link
, &from
->cset_links
, cset_link
)
113 cgroup_migrate_add_src(link
->cset
, to
, &mgctx
);
114 spin_unlock_irq(&css_set_lock
);
116 ret
= cgroup_migrate_prepare_dst(&mgctx
);
121 * Migrate tasks one-by-one until @from is empty. This fails iff
122 * ->can_attach() fails.
125 css_task_iter_start(&from
->self
, 0, &it
);
126 task
= css_task_iter_next(&it
);
128 get_task_struct(task
);
129 css_task_iter_end(&it
);
132 ret
= cgroup_migrate(task
, false, &mgctx
);
134 trace_cgroup_transfer_tasks(to
, task
, false);
135 put_task_struct(task
);
137 } while (task
&& !ret
);
139 cgroup_migrate_finish(&mgctx
);
140 percpu_up_write(&cgroup_threadgroup_rwsem
);
141 mutex_unlock(&cgroup_mutex
);
146 * Stuff for reading the 'tasks'/'procs' files.
148 * Reading this file can return large amounts of data if a cgroup has
149 * *lots* of attached tasks. So it may need several calls to read(),
150 * but we cannot guarantee that the information we produce is correct
151 * unless we produce it entirely atomically.
155 /* which pidlist file are we talking about? */
156 enum cgroup_filetype
{
162 * A pidlist is a list of pids that virtually represents the contents of one
163 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
164 * a pair (one each for procs, tasks) for each pid namespace that's relevant
167 struct cgroup_pidlist
{
169 * used to find which pidlist is wanted. doesn't change as long as
170 * this particular list stays in the list.
172 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
175 /* how many elements the above list has */
177 /* each of these stored in a list by its cgroup */
178 struct list_head links
;
179 /* pointer to the cgroup we belong to, for list removal purposes */
180 struct cgroup
*owner
;
181 /* for delayed destruction */
182 struct delayed_work destroy_dwork
;
186 * The following two functions "fix" the issue where there are more pids
187 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
188 * TODO: replace with a kernel-wide solution to this problem
190 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
191 static void *pidlist_allocate(int count
)
193 if (PIDLIST_TOO_LARGE(count
))
194 return vmalloc(count
* sizeof(pid_t
));
196 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
199 static void pidlist_free(void *p
)
205 * Used to destroy all pidlists lingering waiting for destroy timer. None
206 * should be left afterwards.
208 void cgroup1_pidlist_destroy_all(struct cgroup
*cgrp
)
210 struct cgroup_pidlist
*l
, *tmp_l
;
212 mutex_lock(&cgrp
->pidlist_mutex
);
213 list_for_each_entry_safe(l
, tmp_l
, &cgrp
->pidlists
, links
)
214 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
, 0);
215 mutex_unlock(&cgrp
->pidlist_mutex
);
217 flush_workqueue(cgroup_pidlist_destroy_wq
);
218 BUG_ON(!list_empty(&cgrp
->pidlists
));
221 static void cgroup_pidlist_destroy_work_fn(struct work_struct
*work
)
223 struct delayed_work
*dwork
= to_delayed_work(work
);
224 struct cgroup_pidlist
*l
= container_of(dwork
, struct cgroup_pidlist
,
226 struct cgroup_pidlist
*tofree
= NULL
;
228 mutex_lock(&l
->owner
->pidlist_mutex
);
231 * Destroy iff we didn't get queued again. The state won't change
232 * as destroy_dwork can only be queued while locked.
234 if (!delayed_work_pending(dwork
)) {
236 pidlist_free(l
->list
);
237 put_pid_ns(l
->key
.ns
);
241 mutex_unlock(&l
->owner
->pidlist_mutex
);
246 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
247 * Returns the number of unique elements.
249 static int pidlist_uniq(pid_t
*list
, int length
)
254 * we presume the 0th element is unique, so i starts at 1. trivial
255 * edge cases first; no work needs to be done for either
257 if (length
== 0 || length
== 1)
259 /* src and dest walk down the list; dest counts unique elements */
260 for (src
= 1; src
< length
; src
++) {
261 /* find next unique element */
262 while (list
[src
] == list
[src
-1]) {
267 /* dest always points to where the next unique element goes */
268 list
[dest
] = list
[src
];
276 * The two pid files - task and cgroup.procs - guaranteed that the result
277 * is sorted, which forced this whole pidlist fiasco. As pid order is
278 * different per namespace, each namespace needs differently sorted list,
279 * making it impossible to use, for example, single rbtree of member tasks
280 * sorted by task pointer. As pidlists can be fairly large, allocating one
281 * per open file is dangerous, so cgroup had to implement shared pool of
282 * pidlists keyed by cgroup and namespace.
284 static int cmppid(const void *a
, const void *b
)
286 return *(pid_t
*)a
- *(pid_t
*)b
;
289 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
290 enum cgroup_filetype type
)
292 struct cgroup_pidlist
*l
;
293 /* don't need task_nsproxy() if we're looking at ourself */
294 struct pid_namespace
*ns
= task_active_pid_ns(current
);
296 lockdep_assert_held(&cgrp
->pidlist_mutex
);
298 list_for_each_entry(l
, &cgrp
->pidlists
, links
)
299 if (l
->key
.type
== type
&& l
->key
.ns
== ns
)
305 * find the appropriate pidlist for our purpose (given procs vs tasks)
306 * returns with the lock on that pidlist already held, and takes care
307 * of the use count, or returns NULL with no locks held if we're out of
310 static struct cgroup_pidlist
*cgroup_pidlist_find_create(struct cgroup
*cgrp
,
311 enum cgroup_filetype type
)
313 struct cgroup_pidlist
*l
;
315 lockdep_assert_held(&cgrp
->pidlist_mutex
);
317 l
= cgroup_pidlist_find(cgrp
, type
);
321 /* entry not found; create a new one */
322 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
326 INIT_DELAYED_WORK(&l
->destroy_dwork
, cgroup_pidlist_destroy_work_fn
);
328 /* don't need task_nsproxy() if we're looking at ourself */
329 l
->key
.ns
= get_pid_ns(task_active_pid_ns(current
));
331 list_add(&l
->links
, &cgrp
->pidlists
);
336 * cgroup_task_count - count the number of tasks in a cgroup.
337 * @cgrp: the cgroup in question
339 int cgroup_task_count(const struct cgroup
*cgrp
)
342 struct cgrp_cset_link
*link
;
344 spin_lock_irq(&css_set_lock
);
345 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
346 count
+= link
->cset
->nr_tasks
;
347 spin_unlock_irq(&css_set_lock
);
352 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
354 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
355 struct cgroup_pidlist
**lp
)
359 int pid
, n
= 0; /* used for populating the array */
360 struct css_task_iter it
;
361 struct task_struct
*tsk
;
362 struct cgroup_pidlist
*l
;
364 lockdep_assert_held(&cgrp
->pidlist_mutex
);
367 * If cgroup gets more users after we read count, we won't have
368 * enough space - tough. This race is indistinguishable to the
369 * caller from the case that the additional cgroup users didn't
370 * show up until sometime later on.
372 length
= cgroup_task_count(cgrp
);
373 array
= pidlist_allocate(length
);
376 /* now, populate the array */
377 css_task_iter_start(&cgrp
->self
, 0, &it
);
378 while ((tsk
= css_task_iter_next(&it
))) {
379 if (unlikely(n
== length
))
381 /* get tgid or pid for procs or tasks file respectively */
382 if (type
== CGROUP_FILE_PROCS
)
383 pid
= task_tgid_vnr(tsk
);
385 pid
= task_pid_vnr(tsk
);
386 if (pid
> 0) /* make sure to only use valid results */
389 css_task_iter_end(&it
);
391 /* now sort & (if procs) strip out duplicates */
392 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
393 if (type
== CGROUP_FILE_PROCS
)
394 length
= pidlist_uniq(array
, length
);
396 l
= cgroup_pidlist_find_create(cgrp
, type
);
402 /* store array, freeing old if necessary */
403 pidlist_free(l
->list
);
411 * seq_file methods for the tasks/procs files. The seq_file position is the
412 * next pid to display; the seq_file iterator is a pointer to the pid
413 * in the cgroup->l->list array.
416 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
419 * Initially we receive a position value that corresponds to
420 * one more than the last pid shown (or 0 on the first call or
421 * after a seek to the start). Use a binary-search to find the
422 * next pid to display, if any
424 struct kernfs_open_file
*of
= s
->private;
425 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
426 struct cgroup_pidlist
*l
;
427 enum cgroup_filetype type
= seq_cft(s
)->private;
428 int index
= 0, pid
= *pos
;
431 mutex_lock(&cgrp
->pidlist_mutex
);
434 * !NULL @of->priv indicates that this isn't the first start()
435 * after open. If the matching pidlist is around, we can use that.
436 * Look for it. Note that @of->priv can't be used directly. It
437 * could already have been destroyed.
440 of
->priv
= cgroup_pidlist_find(cgrp
, type
);
443 * Either this is the first start() after open or the matching
444 * pidlist has been destroyed inbetween. Create a new one.
447 ret
= pidlist_array_load(cgrp
, type
,
448 (struct cgroup_pidlist
**)&of
->priv
);
457 while (index
< end
) {
458 int mid
= (index
+ end
) / 2;
459 if (l
->list
[mid
] == pid
) {
462 } else if (l
->list
[mid
] <= pid
)
468 /* If we're off the end of the array, we're done */
469 if (index
>= l
->length
)
471 /* Update the abstract position to be the actual pid that we found */
472 iter
= l
->list
+ index
;
477 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
479 struct kernfs_open_file
*of
= s
->private;
480 struct cgroup_pidlist
*l
= of
->priv
;
483 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
,
484 CGROUP_PIDLIST_DESTROY_DELAY
);
485 mutex_unlock(&seq_css(s
)->cgroup
->pidlist_mutex
);
488 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
490 struct kernfs_open_file
*of
= s
->private;
491 struct cgroup_pidlist
*l
= of
->priv
;
493 pid_t
*end
= l
->list
+ l
->length
;
495 * Advance to the next pid in the array. If this goes off the
507 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
509 seq_printf(s
, "%d\n", *(int *)v
);
514 static ssize_t
__cgroup1_procs_write(struct kernfs_open_file
*of
,
515 char *buf
, size_t nbytes
, loff_t off
,
519 struct task_struct
*task
;
520 const struct cred
*cred
, *tcred
;
523 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
527 task
= cgroup_procs_write_start(buf
, threadgroup
);
528 ret
= PTR_ERR_OR_ZERO(task
);
533 * Even if we're attaching all tasks in the thread group, we only
534 * need to check permissions on one of them.
536 cred
= current_cred();
537 tcred
= get_task_cred(task
);
538 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
539 !uid_eq(cred
->euid
, tcred
->uid
) &&
540 !uid_eq(cred
->euid
, tcred
->suid
))
546 ret
= cgroup_attach_task(cgrp
, task
, threadgroup
);
549 cgroup_procs_write_finish(task
);
551 cgroup_kn_unlock(of
->kn
);
553 return ret
?: nbytes
;
556 static ssize_t
cgroup1_procs_write(struct kernfs_open_file
*of
,
557 char *buf
, size_t nbytes
, loff_t off
)
559 return __cgroup1_procs_write(of
, buf
, nbytes
, off
, true);
562 static ssize_t
cgroup1_tasks_write(struct kernfs_open_file
*of
,
563 char *buf
, size_t nbytes
, loff_t off
)
565 return __cgroup1_procs_write(of
, buf
, nbytes
, off
, false);
568 static ssize_t
cgroup_release_agent_write(struct kernfs_open_file
*of
,
569 char *buf
, size_t nbytes
, loff_t off
)
573 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
575 cgrp
= cgroup_kn_lock_live(of
->kn
, false);
578 spin_lock(&release_agent_path_lock
);
579 strlcpy(cgrp
->root
->release_agent_path
, strstrip(buf
),
580 sizeof(cgrp
->root
->release_agent_path
));
581 spin_unlock(&release_agent_path_lock
);
582 cgroup_kn_unlock(of
->kn
);
586 static int cgroup_release_agent_show(struct seq_file
*seq
, void *v
)
588 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
590 spin_lock(&release_agent_path_lock
);
591 seq_puts(seq
, cgrp
->root
->release_agent_path
);
592 spin_unlock(&release_agent_path_lock
);
597 static int cgroup_sane_behavior_show(struct seq_file
*seq
, void *v
)
599 seq_puts(seq
, "0\n");
603 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
606 return notify_on_release(css
->cgroup
);
609 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
610 struct cftype
*cft
, u64 val
)
613 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
615 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
619 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
622 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
625 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
626 struct cftype
*cft
, u64 val
)
629 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
631 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
635 /* cgroup core interface files for the legacy hierarchies */
636 struct cftype cgroup1_base_files
[] = {
638 .name
= "cgroup.procs",
639 .seq_start
= cgroup_pidlist_start
,
640 .seq_next
= cgroup_pidlist_next
,
641 .seq_stop
= cgroup_pidlist_stop
,
642 .seq_show
= cgroup_pidlist_show
,
643 .private = CGROUP_FILE_PROCS
,
644 .write
= cgroup1_procs_write
,
647 .name
= "cgroup.clone_children",
648 .read_u64
= cgroup_clone_children_read
,
649 .write_u64
= cgroup_clone_children_write
,
652 .name
= "cgroup.sane_behavior",
653 .flags
= CFTYPE_ONLY_ON_ROOT
,
654 .seq_show
= cgroup_sane_behavior_show
,
658 .seq_start
= cgroup_pidlist_start
,
659 .seq_next
= cgroup_pidlist_next
,
660 .seq_stop
= cgroup_pidlist_stop
,
661 .seq_show
= cgroup_pidlist_show
,
662 .private = CGROUP_FILE_TASKS
,
663 .write
= cgroup1_tasks_write
,
666 .name
= "notify_on_release",
667 .read_u64
= cgroup_read_notify_on_release
,
668 .write_u64
= cgroup_write_notify_on_release
,
671 .name
= "release_agent",
672 .flags
= CFTYPE_ONLY_ON_ROOT
,
673 .seq_show
= cgroup_release_agent_show
,
674 .write
= cgroup_release_agent_write
,
675 .max_write_len
= PATH_MAX
- 1,
680 /* Display information about each subsystem and each hierarchy */
681 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
683 struct cgroup_subsys
*ss
;
686 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
688 * ideally we don't want subsystems moving around while we do this.
689 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
690 * subsys/hierarchy state.
692 mutex_lock(&cgroup_mutex
);
694 for_each_subsys(ss
, i
)
695 seq_printf(m
, "%s\t%d\t%d\t%d\n",
696 ss
->legacy_name
, ss
->root
->hierarchy_id
,
697 atomic_read(&ss
->root
->nr_cgrps
),
698 cgroup_ssid_enabled(i
));
700 mutex_unlock(&cgroup_mutex
);
704 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
706 return single_open(file
, proc_cgroupstats_show
, NULL
);
709 const struct file_operations proc_cgroupstats_operations
= {
710 .open
= cgroupstats_open
,
713 .release
= single_release
,
717 * cgroupstats_build - build and fill cgroupstats
718 * @stats: cgroupstats to fill information into
719 * @dentry: A dentry entry belonging to the cgroup for which stats have
722 * Build and fill cgroupstats so that taskstats can export it to user
725 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
727 struct kernfs_node
*kn
= kernfs_node_from_dentry(dentry
);
729 struct css_task_iter it
;
730 struct task_struct
*tsk
;
732 /* it should be kernfs_node belonging to cgroupfs and is a directory */
733 if (dentry
->d_sb
->s_type
!= &cgroup_fs_type
|| !kn
||
734 kernfs_type(kn
) != KERNFS_DIR
)
737 mutex_lock(&cgroup_mutex
);
740 * We aren't being called from kernfs and there's no guarantee on
741 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
742 * @kn->priv is RCU safe. Let's do the RCU dancing.
745 cgrp
= rcu_dereference(*(void __rcu __force
**)&kn
->priv
);
746 if (!cgrp
|| cgroup_is_dead(cgrp
)) {
748 mutex_unlock(&cgroup_mutex
);
753 css_task_iter_start(&cgrp
->self
, 0, &it
);
754 while ((tsk
= css_task_iter_next(&it
))) {
755 switch (tsk
->state
) {
759 case TASK_INTERRUPTIBLE
:
760 stats
->nr_sleeping
++;
762 case TASK_UNINTERRUPTIBLE
:
763 stats
->nr_uninterruptible
++;
769 if (delayacct_is_task_waiting_on_io(tsk
))
774 css_task_iter_end(&it
);
776 mutex_unlock(&cgroup_mutex
);
780 void cgroup1_check_for_release(struct cgroup
*cgrp
)
782 if (notify_on_release(cgrp
) && !cgroup_is_populated(cgrp
) &&
783 !css_has_online_children(&cgrp
->self
) && !cgroup_is_dead(cgrp
))
784 schedule_work(&cgrp
->release_agent_work
);
788 * Notify userspace when a cgroup is released, by running the
789 * configured release agent with the name of the cgroup (path
790 * relative to the root of cgroup file system) as the argument.
792 * Most likely, this user command will try to rmdir this cgroup.
794 * This races with the possibility that some other task will be
795 * attached to this cgroup before it is removed, or that some other
796 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
797 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
798 * unused, and this cgroup will be reprieved from its death sentence,
799 * to continue to serve a useful existence. Next time it's released,
800 * we will get notified again, if it still has 'notify_on_release' set.
802 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
803 * means only wait until the task is successfully execve()'d. The
804 * separate release agent task is forked by call_usermodehelper(),
805 * then control in this thread returns here, without waiting for the
806 * release agent task. We don't bother to wait because the caller of
807 * this routine has no use for the exit status of the release agent
808 * task, so no sense holding our caller up for that.
810 void cgroup1_release_agent(struct work_struct
*work
)
812 struct cgroup
*cgrp
=
813 container_of(work
, struct cgroup
, release_agent_work
);
814 char *pathbuf
= NULL
, *agentbuf
= NULL
;
815 char *argv
[3], *envp
[3];
818 mutex_lock(&cgroup_mutex
);
820 pathbuf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
821 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
822 if (!pathbuf
|| !agentbuf
)
825 spin_lock_irq(&css_set_lock
);
826 ret
= cgroup_path_ns_locked(cgrp
, pathbuf
, PATH_MAX
, &init_cgroup_ns
);
827 spin_unlock_irq(&css_set_lock
);
828 if (ret
< 0 || ret
>= PATH_MAX
)
835 /* minimal command environment */
837 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
840 mutex_unlock(&cgroup_mutex
);
841 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
844 mutex_unlock(&cgroup_mutex
);
851 * cgroup_rename - Only allow simple rename of directories in place.
853 static int cgroup1_rename(struct kernfs_node
*kn
, struct kernfs_node
*new_parent
,
854 const char *new_name_str
)
856 struct cgroup
*cgrp
= kn
->priv
;
859 if (kernfs_type(kn
) != KERNFS_DIR
)
861 if (kn
->parent
!= new_parent
)
865 * We're gonna grab cgroup_mutex which nests outside kernfs
866 * active_ref. kernfs_rename() doesn't require active_ref
867 * protection. Break them before grabbing cgroup_mutex.
869 kernfs_break_active_protection(new_parent
);
870 kernfs_break_active_protection(kn
);
872 mutex_lock(&cgroup_mutex
);
874 ret
= kernfs_rename(kn
, new_parent
, new_name_str
);
876 trace_cgroup_rename(cgrp
);
878 mutex_unlock(&cgroup_mutex
);
880 kernfs_unbreak_active_protection(kn
);
881 kernfs_unbreak_active_protection(new_parent
);
885 static int cgroup1_show_options(struct seq_file
*seq
, struct kernfs_root
*kf_root
)
887 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
888 struct cgroup_subsys
*ss
;
891 for_each_subsys(ss
, ssid
)
892 if (root
->subsys_mask
& (1 << ssid
))
893 seq_show_option(seq
, ss
->legacy_name
, NULL
);
894 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
895 seq_puts(seq
, ",noprefix");
896 if (root
->flags
& CGRP_ROOT_XATTR
)
897 seq_puts(seq
, ",xattr");
898 if (root
->flags
& CGRP_ROOT_CPUSET_V2_MODE
)
899 seq_puts(seq
, ",cpuset_v2_mode");
901 spin_lock(&release_agent_path_lock
);
902 if (strlen(root
->release_agent_path
))
903 seq_show_option(seq
, "release_agent",
904 root
->release_agent_path
);
905 spin_unlock(&release_agent_path_lock
);
907 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->cgrp
.flags
))
908 seq_puts(seq
, ",clone_children");
909 if (strlen(root
->name
))
910 seq_show_option(seq
, "name", root
->name
);
914 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
916 char *token
, *o
= data
;
917 bool all_ss
= false, one_ss
= false;
919 struct cgroup_subsys
*ss
;
923 #ifdef CONFIG_CPUSETS
924 mask
= ~((u16
)1 << cpuset_cgrp_id
);
927 memset(opts
, 0, sizeof(*opts
));
929 while ((token
= strsep(&o
, ",")) != NULL
) {
934 if (!strcmp(token
, "none")) {
935 /* Explicitly have no subsystems */
939 if (!strcmp(token
, "all")) {
940 /* Mutually exclusive option 'all' + subsystem name */
946 if (!strcmp(token
, "noprefix")) {
947 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
950 if (!strcmp(token
, "clone_children")) {
951 opts
->cpuset_clone_children
= true;
954 if (!strcmp(token
, "cpuset_v2_mode")) {
955 opts
->flags
|= CGRP_ROOT_CPUSET_V2_MODE
;
958 if (!strcmp(token
, "xattr")) {
959 opts
->flags
|= CGRP_ROOT_XATTR
;
962 if (!strncmp(token
, "release_agent=", 14)) {
963 /* Specifying two release agents is forbidden */
964 if (opts
->release_agent
)
966 opts
->release_agent
=
967 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
968 if (!opts
->release_agent
)
972 if (!strncmp(token
, "name=", 5)) {
973 const char *name
= token
+ 5;
974 /* Can't specify an empty name */
977 /* Must match [\w.-]+ */
978 for (i
= 0; i
< strlen(name
); i
++) {
982 if ((c
== '.') || (c
== '-') || (c
== '_'))
986 /* Specifying two names is forbidden */
989 opts
->name
= kstrndup(name
,
990 MAX_CGROUP_ROOT_NAMELEN
- 1,
998 for_each_subsys(ss
, i
) {
999 if (strcmp(token
, ss
->legacy_name
))
1001 if (!cgroup_ssid_enabled(i
))
1003 if (cgroup1_ssid_disabled(i
))
1006 /* Mutually exclusive option 'all' + subsystem name */
1009 opts
->subsys_mask
|= (1 << i
);
1014 if (i
== CGROUP_SUBSYS_COUNT
)
1019 * If the 'all' option was specified select all the subsystems,
1020 * otherwise if 'none', 'name=' and a subsystem name options were
1021 * not specified, let's default to 'all'
1023 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1024 for_each_subsys(ss
, i
)
1025 if (cgroup_ssid_enabled(i
) && !cgroup1_ssid_disabled(i
))
1026 opts
->subsys_mask
|= (1 << i
);
1029 * We either have to specify by name or by subsystems. (So all
1030 * empty hierarchies must have a name).
1032 if (!opts
->subsys_mask
&& !opts
->name
)
1036 * Option noprefix was introduced just for backward compatibility
1037 * with the old cpuset, so we allow noprefix only if mounting just
1038 * the cpuset subsystem.
1040 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1043 /* Can't specify "none" and some subsystems */
1044 if (opts
->subsys_mask
&& opts
->none
)
1050 static int cgroup1_remount(struct kernfs_root
*kf_root
, int *flags
, char *data
)
1053 struct cgroup_root
*root
= cgroup_root_from_kf(kf_root
);
1054 struct cgroup_sb_opts opts
;
1055 u16 added_mask
, removed_mask
;
1057 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1059 /* See what subsystems are wanted */
1060 ret
= parse_cgroupfs_options(data
, &opts
);
1064 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1065 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
1066 task_tgid_nr(current
), current
->comm
);
1068 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1069 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1071 /* Don't allow flags or name to change at remount */
1072 if ((opts
.flags
^ root
->flags
) ||
1073 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1074 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
1075 opts
.flags
, opts
.name
?: "", root
->flags
, root
->name
);
1080 /* remounting is not allowed for populated hierarchies */
1081 if (!list_empty(&root
->cgrp
.self
.children
)) {
1086 ret
= rebind_subsystems(root
, added_mask
);
1090 WARN_ON(rebind_subsystems(&cgrp_dfl_root
, removed_mask
));
1092 if (opts
.release_agent
) {
1093 spin_lock(&release_agent_path_lock
);
1094 strcpy(root
->release_agent_path
, opts
.release_agent
);
1095 spin_unlock(&release_agent_path_lock
);
1098 trace_cgroup_remount(root
);
1101 kfree(opts
.release_agent
);
1103 mutex_unlock(&cgroup_mutex
);
1107 struct kernfs_syscall_ops cgroup1_kf_syscall_ops
= {
1108 .rename
= cgroup1_rename
,
1109 .show_options
= cgroup1_show_options
,
1110 .remount_fs
= cgroup1_remount
,
1111 .mkdir
= cgroup_mkdir
,
1112 .rmdir
= cgroup_rmdir
,
1113 .show_path
= cgroup_show_path
,
1116 struct dentry
*cgroup1_mount(struct file_system_type
*fs_type
, int flags
,
1117 void *data
, unsigned long magic
,
1118 struct cgroup_namespace
*ns
)
1120 struct super_block
*pinned_sb
= NULL
;
1121 struct cgroup_sb_opts opts
;
1122 struct cgroup_root
*root
;
1123 struct cgroup_subsys
*ss
;
1124 struct dentry
*dentry
;
1126 bool new_root
= false;
1128 cgroup_lock_and_drain_offline(&cgrp_dfl_root
.cgrp
);
1130 /* First find the desired set of subsystems */
1131 ret
= parse_cgroupfs_options(data
, &opts
);
1136 * Destruction of cgroup root is asynchronous, so subsystems may
1137 * still be dying after the previous unmount. Let's drain the
1138 * dying subsystems. We just need to ensure that the ones
1139 * unmounted previously finish dying and don't care about new ones
1140 * starting. Testing ref liveliness is good enough.
1142 for_each_subsys(ss
, i
) {
1143 if (!(opts
.subsys_mask
& (1 << i
)) ||
1144 ss
->root
== &cgrp_dfl_root
)
1147 if (!percpu_ref_tryget_live(&ss
->root
->cgrp
.self
.refcnt
)) {
1148 mutex_unlock(&cgroup_mutex
);
1150 ret
= restart_syscall();
1153 cgroup_put(&ss
->root
->cgrp
);
1156 for_each_root(root
) {
1157 bool name_match
= false;
1159 if (root
== &cgrp_dfl_root
)
1163 * If we asked for a name then it must match. Also, if
1164 * name matches but sybsys_mask doesn't, we should fail.
1165 * Remember whether name matched.
1168 if (strcmp(opts
.name
, root
->name
))
1174 * If we asked for subsystems (or explicitly for no
1175 * subsystems) then they must match.
1177 if ((opts
.subsys_mask
|| opts
.none
) &&
1178 (opts
.subsys_mask
!= root
->subsys_mask
)) {
1185 if (root
->flags
^ opts
.flags
)
1186 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
1189 * We want to reuse @root whose lifetime is governed by its
1190 * ->cgrp. Let's check whether @root is alive and keep it
1191 * that way. As cgroup_kill_sb() can happen anytime, we
1192 * want to block it by pinning the sb so that @root doesn't
1193 * get killed before mount is complete.
1195 * With the sb pinned, tryget_live can reliably indicate
1196 * whether @root can be reused. If it's being killed,
1197 * drain it. We can use wait_queue for the wait but this
1198 * path is super cold. Let's just sleep a bit and retry.
1200 pinned_sb
= kernfs_pin_sb(root
->kf_root
, NULL
);
1201 if (IS_ERR(pinned_sb
) ||
1202 !percpu_ref_tryget_live(&root
->cgrp
.self
.refcnt
)) {
1203 mutex_unlock(&cgroup_mutex
);
1204 if (!IS_ERR_OR_NULL(pinned_sb
))
1205 deactivate_super(pinned_sb
);
1207 ret
= restart_syscall();
1216 * No such thing, create a new one. name= matching without subsys
1217 * specification is allowed for already existing hierarchies but we
1218 * can't create new one without subsys specification.
1220 if (!opts
.subsys_mask
&& !opts
.none
) {
1225 /* Hierarchies may only be created in the initial cgroup namespace. */
1226 if (ns
!= &init_cgroup_ns
) {
1231 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1238 init_cgroup_root(root
, &opts
);
1240 ret
= cgroup_setup_root(root
, opts
.subsys_mask
, PERCPU_REF_INIT_DEAD
);
1242 cgroup_free_root(root
);
1245 mutex_unlock(&cgroup_mutex
);
1247 kfree(opts
.release_agent
);
1251 return ERR_PTR(ret
);
1253 dentry
= cgroup_do_mount(&cgroup_fs_type
, flags
, root
,
1254 CGROUP_SUPER_MAGIC
, ns
);
1257 * There's a race window after we release cgroup_mutex and before
1258 * allocating a superblock. Make sure a concurrent process won't
1259 * be able to re-use the root during this window by delaying the
1260 * initialization of root refcnt.
1263 mutex_lock(&cgroup_mutex
);
1264 percpu_ref_reinit(&root
->cgrp
.self
.refcnt
);
1265 mutex_unlock(&cgroup_mutex
);
1269 * If @pinned_sb, we're reusing an existing root and holding an
1270 * extra ref on its sb. Mount is complete. Put the extra ref.
1273 deactivate_super(pinned_sb
);
1278 static int __init
cgroup1_wq_init(void)
1281 * Used to destroy pidlists and separate to serve as flush domain.
1282 * Cap @max_active to 1 too.
1284 cgroup_pidlist_destroy_wq
= alloc_workqueue("cgroup_pidlist_destroy",
1286 BUG_ON(!cgroup_pidlist_destroy_wq
);
1289 core_initcall(cgroup1_wq_init
);
1291 static int __init
cgroup_no_v1(char *str
)
1293 struct cgroup_subsys
*ss
;
1297 while ((token
= strsep(&str
, ",")) != NULL
) {
1301 if (!strcmp(token
, "all")) {
1302 cgroup_no_v1_mask
= U16_MAX
;
1306 for_each_subsys(ss
, i
) {
1307 if (strcmp(token
, ss
->name
) &&
1308 strcmp(token
, ss
->legacy_name
))
1311 cgroup_no_v1_mask
|= 1 << i
;
1316 __setup("cgroup_no_v1=", cgroup_no_v1
);