/* * pidlists linger the following amount before being destroyed. The goal * is avoiding frequent destruction in the middle of consecutive read calls * Expiring in the middle is a performance problem not a correctness one. * 1 sec should be enough.
*/ #define CGROUP_PIDLIST_DESTROY_DELAY HZ
/* Controllers blocked by the commandline in v1 */ static u16 cgroup_no_v1_mask;
/* disable named v1 mounts */ staticbool cgroup_no_v1_named;
/* Show unavailable controllers in /proc/cgroups */ staticbool proc_show_all;
/* * pidlist destructions need to be flushed on cgroup destruction. Use a * separate workqueue as flush domain.
*/ staticstruct workqueue_struct *cgroup_pidlist_destroy_wq;
staticbool cgroup1_subsys_absent(struct cgroup_subsys *ss)
{ /* Check also dfl_cftypes for file-less controllers, i.e. perf_event */ return ss->legacy_cftypes == NULL && ss->dfl_cftypes;
}
/** * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' * @from: attach to all cgroups of a given task * @tsk: the task to be attached * * Return: %0 on success or a negative errno code on failure
*/ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
{ struct cgroup_root *root; int retval = 0;
/** * cgroup_transfer_tasks - move tasks from one cgroup to another * @to: cgroup to which the tasks will be moved * @from: cgroup in which the tasks currently reside * * Locking rules between cgroup_post_fork() and the migration path * guarantee that, if a task is forking while being migrated, the new child * is guaranteed to be either visible in the source cgroup after the * parent's migration is complete or put into the target cgroup. No task * can slip out of migration through forking. * * Return: %0 on success or a negative errno code on failure
*/ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
{
DEFINE_CGROUP_MGCTX(mgctx); struct cgrp_cset_link *link; struct css_task_iter it; struct task_struct *task; int ret;
if (cgroup_on_dfl(to)) return -EINVAL;
ret = cgroup_migrate_vet_dst(to); if (ret) return ret;
cgroup_lock();
cgroup_attach_lock(true);
/* all tasks in @from are being moved, all csets are source */
spin_lock_irq(&css_set_lock);
list_for_each_entry(link, &from->cset_links, cset_link)
cgroup_migrate_add_src(link->cset, to, &mgctx);
spin_unlock_irq(&css_set_lock);
ret = cgroup_migrate_prepare_dst(&mgctx); if (ret) goto out_err;
/* * Migrate tasks one-by-one until @from is empty. This fails iff * ->can_attach() fails.
*/ do {
css_task_iter_start(&from->self, 0, &it);
do {
task = css_task_iter_next(&it);
} while (task && (task->flags & PF_EXITING));
if (task)
get_task_struct(task);
css_task_iter_end(&it);
if (task) {
ret = cgroup_migrate(task, false, &mgctx); if (!ret)
TRACE_CGROUP_PATH(transfer_tasks, to, task, false);
put_task_struct(task);
}
} while (task && !ret);
out_err:
cgroup_migrate_finish(&mgctx);
cgroup_attach_unlock(true);
cgroup_unlock(); return ret;
}
/* * Stuff for reading the 'tasks'/'procs' files. * * Reading this file can return large amounts of data if a cgroup has * *lots* of attached tasks. So it may need several calls to read(), * but we cannot guarantee that the information we produce is correct * unless we produce it entirely atomically. *
*/
/* which pidlist file are we talking about? */ enum cgroup_filetype {
CGROUP_FILE_PROCS,
CGROUP_FILE_TASKS,
};
/* * A pidlist is a list of pids that virtually represents the contents of one * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, * a pair (one each for procs, tasks) for each pid namespace that's relevant * to the cgroup.
*/ struct cgroup_pidlist { /* * used to find which pidlist is wanted. doesn't change as long as * this particular list stays in the list.
*/ struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; /* array of xids */
pid_t *list; /* how many elements the above list has */ int length; /* each of these stored in a list by its cgroup */ struct list_head links; /* pointer to the cgroup we belong to, for list removal purposes */ struct cgroup *owner; /* for delayed destruction */ struct delayed_work destroy_dwork;
};
/* * Used to destroy all pidlists lingering waiting for destroy timer. None * should be left afterwards.
*/ void cgroup1_pidlist_destroy_all(struct cgroup *cgrp)
{ struct cgroup_pidlist *l, *tmp_l;
/* * Destroy iff we didn't get queued again. The state won't change * as destroy_dwork can only be queued while locked.
*/ if (!delayed_work_pending(dwork)) {
list_del(&l->links);
kvfree(l->list);
put_pid_ns(l->key.ns);
tofree = l;
}
/* * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries * Returns the number of unique elements.
*/ staticint pidlist_uniq(pid_t *list, int length)
{ int src, dest = 1;
/* * we presume the 0th element is unique, so i starts at 1. trivial * edge cases first; no work needs to be done for either
*/ if (length == 0 || length == 1) return length; /* src and dest walk down the list; dest counts unique elements */ for (src = 1; src < length; src++) { /* find next unique element */ while (list[src] == list[src-1]) {
src++; if (src == length) goto after;
} /* dest always points to where the next unique element goes */
list[dest] = list[src];
dest++;
}
after: return dest;
}
/* * The two pid files - task and cgroup.procs - guaranteed that the result * is sorted, which forced this whole pidlist fiasco. As pid order is * different per namespace, each namespace needs differently sorted list, * making it impossible to use, for example, single rbtree of member tasks * sorted by task pointer. As pidlists can be fairly large, allocating one * per open file is dangerous, so cgroup had to implement shared pool of * pidlists keyed by cgroup and namespace.
*/ staticint cmppid(constvoid *a, constvoid *b)
{ return *(pid_t *)a - *(pid_t *)b;
}
staticstruct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, enum cgroup_filetype type)
{ struct cgroup_pidlist *l; /* don't need task_nsproxy() if we're looking at ourself */ struct pid_namespace *ns = task_active_pid_ns(current);
lockdep_assert_held(&cgrp->pidlist_mutex);
list_for_each_entry(l, &cgrp->pidlists, links) if (l->key.type == type && l->key.ns == ns) return l; return NULL;
}
/* * find the appropriate pidlist for our purpose (given procs vs tasks) * returns with the lock on that pidlist already held, and takes care * of the use count, or returns NULL with no locks held if we're out of * memory.
*/ staticstruct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, enum cgroup_filetype type)
{ struct cgroup_pidlist *l;
lockdep_assert_held(&cgrp->pidlist_mutex);
l = cgroup_pidlist_find(cgrp, type); if (l) return l;
/* entry not found; create a new one */
l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); if (!l) return l;
INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
l->key.type = type; /* don't need task_nsproxy() if we're looking at ourself */
l->key.ns = get_pid_ns(task_active_pid_ns(current));
l->owner = cgrp;
list_add(&l->links, &cgrp->pidlists); return l;
}
/* * Load a cgroup's pidarray with either procs' tgids or tasks' pids
*/ staticint pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, struct cgroup_pidlist **lp)
{
pid_t *array; int length; int pid, n = 0; /* used for populating the array */ struct css_task_iter it; struct task_struct *tsk; struct cgroup_pidlist *l;
lockdep_assert_held(&cgrp->pidlist_mutex);
/* * If cgroup gets more users after we read count, we won't have * enough space - tough. This race is indistinguishable to the * caller from the case that the additional cgroup users didn't * show up until sometime later on.
*/
length = cgroup_task_count(cgrp);
array = kvmalloc_array(length, sizeof(pid_t), GFP_KERNEL); if (!array) return -ENOMEM; /* now, populate the array */
css_task_iter_start(&cgrp->self, 0, &it); while ((tsk = css_task_iter_next(&it))) { if (unlikely(n == length)) break; /* get tgid or pid for procs or tasks file respectively */ if (type == CGROUP_FILE_PROCS)
pid = task_tgid_vnr(tsk); else
pid = task_pid_vnr(tsk); if (pid > 0) /* make sure to only use valid results */
array[n++] = pid;
}
css_task_iter_end(&it);
length = n; /* now sort & strip out duplicates (tgids or recycled thread PIDs) */
sort(array, length, sizeof(pid_t), cmppid, NULL);
length = pidlist_uniq(array, length);
l = cgroup_pidlist_find_create(cgrp, type); if (!l) {
kvfree(array); return -ENOMEM;
}
/* store array, freeing old if necessary */
kvfree(l->list);
l->list = array;
l->length = length;
*lp = l; return 0;
}
/* * seq_file methods for the tasks/procs files. The seq_file position is the * next pid to display; the seq_file iterator is a pointer to the pid * in the cgroup->l->list array.
*/
staticvoid *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
{ /* * Initially we receive a position value that corresponds to * one more than the last pid shown (or 0 on the first call or * after a seek to the start). Use a binary-search to find the * next pid to display, if any
*/ struct kernfs_open_file *of = s->private; struct cgroup_file_ctx *ctx = of->priv; struct cgroup *cgrp = seq_css(s)->cgroup; struct cgroup_pidlist *l; enum cgroup_filetype type = seq_cft(s)->private; int index = 0, pid = *pos; int *iter, ret;
mutex_lock(&cgrp->pidlist_mutex);
/* * !NULL @ctx->procs1.pidlist indicates that this isn't the first * start() after open. If the matching pidlist is around, we can use * that. Look for it. Note that @ctx->procs1.pidlist can't be used * directly. It could already have been destroyed.
*/ if (ctx->procs1.pidlist)
ctx->procs1.pidlist = cgroup_pidlist_find(cgrp, type);
/* * Either this is the first start() after open or the matching * pidlist has been destroyed inbetween. Create a new one.
*/ if (!ctx->procs1.pidlist) {
ret = pidlist_array_load(cgrp, type, &ctx->procs1.pidlist); if (ret) return ERR_PTR(ret);
}
l = ctx->procs1.pidlist;
if (pid) { int end = l->length;
while (index < end) { int mid = (index + end) / 2; if (l->list[mid] == pid) {
index = mid; break;
} elseif (l->list[mid] < pid)
index = mid + 1; else
end = mid;
}
} /* If we're off the end of the array, we're done */ if (index >= l->length) return NULL; /* Update the abstract position to be the actual pid that we found */
iter = l->list + index;
*pos = *iter; return iter;
}
cgrp = cgroup_kn_lock_live(of->kn, false); if (!cgrp) return -ENODEV;
task = cgroup_procs_write_start(buf, threadgroup, &locked);
ret = PTR_ERR_OR_ZERO(task); if (ret) goto out_unlock;
/* * Even if we're attaching all tasks in the thread group, we only need * to check permissions on one of them. Check permissions using the * credentials from file open to protect against inherited fd attacks.
*/
cred = of->file->f_cred;
tcred = get_task_cred(task); if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
!uid_eq(cred->euid, tcred->uid) &&
!uid_eq(cred->euid, tcred->suid))
ret = -EACCES;
put_cred(tcred); if (ret) goto out_finish;
ret = cgroup_attach_task(cgrp, task, threadgroup);
/* * Release agent gets called with all capabilities, * require capabilities to set release agent.
*/
ctx = of->priv; if ((ctx->ns->user_ns != &init_user_ns) ||
!file_ns_capable(of->file, &init_user_ns, CAP_SYS_ADMIN)) return -EPERM;
/* Display information about each subsystem and each hierarchy */ int proc_cgroupstats_show(struct seq_file *m, void *v)
{ struct cgroup_subsys *ss; bool cgrp_v1_visible = false; int i;
seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); /* * Grab the subsystems state racily. No need to add avenue to * cgroup_mutex contention.
*/
if (cgrp_dfl_visible && !cgrp_v1_visible)
pr_info_once("/proc/cgroups lists only v1 controllers, use cgroup.controllers of root cgroup for v2 info\n");
return 0;
}
/** * cgroupstats_build - build and fill cgroupstats * @stats: cgroupstats to fill information into * @dentry: A dentry entry belonging to the cgroup for which stats have * been requested. * * Build and fill cgroupstats so that taskstats can export it to user * space. * * Return: %0 on success or a negative errno code on failure
*/ int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
{ struct kernfs_node *kn = kernfs_node_from_dentry(dentry); struct cgroup *cgrp; struct css_task_iter it; struct task_struct *tsk;
/* it should be kernfs_node belonging to cgroupfs and is a directory */ if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
kernfs_type(kn) != KERNFS_DIR) return -EINVAL;
/* * We aren't being called from kernfs and there's no guarantee on * @kn->priv's validity. For this and css_tryget_online_from_dir(), * @kn->priv is RCU safe. Let's do the RCU dancing.
*/
rcu_read_lock();
cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); if (!cgrp || !cgroup_tryget(cgrp)) {
rcu_read_unlock(); return -ENOENT;
}
rcu_read_unlock();
css_task_iter_start(&cgrp->self, 0, &it); while ((tsk = css_task_iter_next(&it))) { switch (READ_ONCE(tsk->__state)) { case TASK_RUNNING:
stats->nr_running++; break; case TASK_INTERRUPTIBLE:
stats->nr_sleeping++; break; case TASK_UNINTERRUPTIBLE:
stats->nr_uninterruptible++; break; case TASK_STOPPED:
stats->nr_stopped++; break; default: if (tsk->in_iowait)
stats->nr_io_wait++; break;
}
}
css_task_iter_end(&it);
/* * Notify userspace when a cgroup is released, by running the * configured release agent with the name of the cgroup (path * relative to the root of cgroup file system) as the argument. * * Most likely, this user command will try to rmdir this cgroup. * * This races with the possibility that some other task will be * attached to this cgroup before it is removed, or that some other * user task will 'mkdir' a child cgroup of this cgroup. That's ok. * The presumed 'rmdir' will fail quietly if this cgroup is no longer * unused, and this cgroup will be reprieved from its death sentence, * to continue to serve a useful existence. Next time it's released, * we will get notified again, if it still has 'notify_on_release' set. * * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which * means only wait until the task is successfully execve()'d. The * separate release agent task is forked by call_usermodehelper(), * then control in this thread returns here, without waiting for the * release agent task. We don't bother to wait because the caller of * this routine has no use for the exit status of the release agent * task, so no sense holding our caller up for that.
*/ void cgroup1_release_agent(struct work_struct *work)
{ struct cgroup *cgrp =
container_of(work, struct cgroup, release_agent_work); char *pathbuf, *agentbuf; char *argv[3], *envp[3]; int ret;
/* snoop agent path and exit early if empty */ if (!cgrp->root->release_agent_path[0]) return;
/* * In absence of 'none', 'name=' and subsystem name options, * let's default to 'all'.
*/ if (!ctx->subsys_mask && !ctx->none && !ctx->name)
ctx->all_ss = true;
if (ctx->all_ss) { /* Mutually exclusive option 'all' + subsystem name */ if (ctx->subsys_mask) return invalfc(fc, "subsys name conflicts with all"); /* 'all' => select all the subsystems */
ctx->subsys_mask = enabled;
}
/* * We either have to specify by name or by subsystems. (So all * empty hierarchies must have a name).
*/ if (!ctx->subsys_mask && !ctx->name) return invalfc(fc, "Need name or subsystem set");
/* * Option noprefix was introduced just for backward compatibility * with the old cpuset, so we allow noprefix only if mounting just * the cpuset subsystem.
*/ if ((ctx->flags & CGRP_ROOT_NOPREFIX) && (ctx->subsys_mask & mask)) return invalfc(fc, "noprefix used incorrectly");
/* Can't specify "none" and some subsystems */ if (ctx->subsys_mask && ctx->none) return invalfc(fc, "none used incorrectly");
return 0;
}
int cgroup1_reconfigure(struct fs_context *fc)
{ struct cgroup_fs_context *ctx = cgroup_fc2context(fc); struct kernfs_root *kf_root = kernfs_root_from_sb(fc->root->d_sb); struct cgroup_root *root = cgroup_root_from_kf(kf_root); int ret = 0;
u16 added_mask, removed_mask;
/* * The guts of cgroup1 mount - find or create cgroup_root to use. * Called with cgroup_mutex held; returns 0 on success, -E... on * error and positive - in case when the candidate is busy dying. * On success it stashes a reference to cgroup_root into given * cgroup_fs_context; that reference is *NOT* counting towards the * cgroup_root refcount.
*/ staticint cgroup1_root_to_use(struct fs_context *fc)
{ struct cgroup_fs_context *ctx = cgroup_fc2context(fc); struct cgroup_root *root; struct cgroup_subsys *ss; int i, ret;
/* First find the desired set of subsystems */
ret = check_cgroupfs_options(fc); if (ret) return ret;
/* * Destruction of cgroup root is asynchronous, so subsystems may * still be dying after the previous unmount. Let's drain the * dying subsystems. We just need to ensure that the ones * unmounted previously finish dying and don't care about new ones * starting. Testing ref liveliness is good enough.
*/
for_each_subsys(ss, i) { if (!(ctx->subsys_mask & (1 << i)) ||
ss->root == &cgrp_dfl_root) continue;
if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) return 1; /* restart */
cgroup_put(&ss->root->cgrp);
}
for_each_root(root) { bool name_match = false;
if (root == &cgrp_dfl_root) continue;
/* * If we asked for a name then it must match. Also, if * name matches but sybsys_mask doesn't, we should fail. * Remember whether name matched.
*/ if (ctx->name) { if (strcmp(ctx->name, root->name)) continue;
name_match = true;
}
/* * If we asked for subsystems (or explicitly for no * subsystems) then they must match.
*/ if ((ctx->subsys_mask || ctx->none) &&
(ctx->subsys_mask != root->subsys_mask)) { if (!name_match) continue; return -EBUSY;
}
if (root->flags ^ ctx->flags)
pr_warn("new mount options do not match the existing superblock, will be ignored\n");
ctx->root = root; return 0;
}
/* * No such thing, create a new one. name= matching without subsys * specification is allowed for already existing hierarchies but we * can't create new one without subsys specification.
*/ if (!ctx->subsys_mask && !ctx->none) return invalfc(fc, "No subsys list or none specified");
/* Hierarchies may only be created in the initial cgroup namespace. */ if (ctx->ns != &init_cgroup_ns) return -EPERM;
root = kzalloc(sizeof(*root), GFP_KERNEL); if (!root) return -ENOMEM;
ctx->root = root;
init_cgroup_root(ctx);
ret = cgroup_setup_root(root, ctx->subsys_mask); if (!ret)
cgroup_favor_dynmods(root, ctx->flags & CGRP_ROOT_FAVOR_DYNMODS); else
cgroup_free_root(root);
return ret;
}
int cgroup1_get_tree(struct fs_context *fc)
{ struct cgroup_fs_context *ctx = cgroup_fc2context(fc); int ret;
/* Check if the caller has permission to mount. */ if (!ns_capable(ctx->ns->user_ns, CAP_SYS_ADMIN)) return -EPERM;
/** * task_get_cgroup1 - Acquires the associated cgroup of a task within a * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its * hierarchy ID. * @tsk: The target task * @hierarchy_id: The ID of a cgroup1 hierarchy * * On success, the cgroup is returned. On failure, ERR_PTR is returned. * We limit it to cgroup1 only.
*/ struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id)
{ struct cgroup *cgrp = ERR_PTR(-ENOENT); struct cgroup_root *root; unsignedlong flags;
staticint __init cgroup1_wq_init(void)
{ /* * Used to destroy pidlists and separate to serve as flush domain. * Cap @max_active to 1 too.
*/
cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
0, 1);
BUG_ON(!cgroup_pidlist_destroy_wq); return 0;
}
core_initcall(cgroup1_wq_init);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.