cgroup: cgroup.{procs,threads} factor out common parts
The functions cgroup_threads_write and cgroup_procs_write are almost identical. In order to reduce duplication, factor out the common code in similar fashion we already do for other threadgroup/task functions. No functional changes are intended. Suggested-by: Hao Lee <haolee.swjtu@gmail.com> Signed-off-by: Michal Koutný <mkoutny@suse.com> Reviewed-by: Daniel Jordan <daniel.m.jordan@oracle.com> Signed-off-by: Tejun Heo <tj@kernel.org>
This commit is contained in:
@ -4726,8 +4726,8 @@ static int cgroup_attach_permissions(struct cgroup *src_cgrp,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
|
static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
|
||||||
char *buf, size_t nbytes, loff_t off)
|
bool threadgroup)
|
||||||
{
|
{
|
||||||
struct cgroup *src_cgrp, *dst_cgrp;
|
struct cgroup *src_cgrp, *dst_cgrp;
|
||||||
struct task_struct *task;
|
struct task_struct *task;
|
||||||
@ -4738,7 +4738,7 @@ static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
|
|||||||
if (!dst_cgrp)
|
if (!dst_cgrp)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
||||||
task = cgroup_procs_write_start(buf, true, &locked);
|
task = cgroup_procs_write_start(buf, threadgroup, &locked);
|
||||||
ret = PTR_ERR_OR_ZERO(task);
|
ret = PTR_ERR_OR_ZERO(task);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto out_unlock;
|
goto out_unlock;
|
||||||
@ -4748,19 +4748,26 @@ static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
|
|||||||
src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
|
src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
|
||||||
spin_unlock_irq(&css_set_lock);
|
spin_unlock_irq(&css_set_lock);
|
||||||
|
|
||||||
|
/* process and thread migrations follow same delegation rule */
|
||||||
ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
|
ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
|
||||||
of->file->f_path.dentry->d_sb, true);
|
of->file->f_path.dentry->d_sb, threadgroup);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto out_finish;
|
goto out_finish;
|
||||||
|
|
||||||
ret = cgroup_attach_task(dst_cgrp, task, true);
|
ret = cgroup_attach_task(dst_cgrp, task, threadgroup);
|
||||||
|
|
||||||
out_finish:
|
out_finish:
|
||||||
cgroup_procs_write_finish(task, locked);
|
cgroup_procs_write_finish(task, locked);
|
||||||
out_unlock:
|
out_unlock:
|
||||||
cgroup_kn_unlock(of->kn);
|
cgroup_kn_unlock(of->kn);
|
||||||
|
|
||||||
return ret ?: nbytes;
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
|
||||||
|
char *buf, size_t nbytes, loff_t off)
|
||||||
|
{
|
||||||
|
return __cgroup_procs_write(of, buf, true) ?: nbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
|
static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
|
||||||
@ -4771,41 +4778,7 @@ static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
|
|||||||
static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
|
static ssize_t cgroup_threads_write(struct kernfs_open_file *of,
|
||||||
char *buf, size_t nbytes, loff_t off)
|
char *buf, size_t nbytes, loff_t off)
|
||||||
{
|
{
|
||||||
struct cgroup *src_cgrp, *dst_cgrp;
|
return __cgroup_procs_write(of, buf, false) ?: nbytes;
|
||||||
struct task_struct *task;
|
|
||||||
ssize_t ret;
|
|
||||||
bool locked;
|
|
||||||
|
|
||||||
buf = strstrip(buf);
|
|
||||||
|
|
||||||
dst_cgrp = cgroup_kn_lock_live(of->kn, false);
|
|
||||||
if (!dst_cgrp)
|
|
||||||
return -ENODEV;
|
|
||||||
|
|
||||||
task = cgroup_procs_write_start(buf, false, &locked);
|
|
||||||
ret = PTR_ERR_OR_ZERO(task);
|
|
||||||
if (ret)
|
|
||||||
goto out_unlock;
|
|
||||||
|
|
||||||
/* find the source cgroup */
|
|
||||||
spin_lock_irq(&css_set_lock);
|
|
||||||
src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
|
|
||||||
spin_unlock_irq(&css_set_lock);
|
|
||||||
|
|
||||||
/* thread migrations follow the cgroup.procs delegation rule */
|
|
||||||
ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
|
|
||||||
of->file->f_path.dentry->d_sb, false);
|
|
||||||
if (ret)
|
|
||||||
goto out_finish;
|
|
||||||
|
|
||||||
ret = cgroup_attach_task(dst_cgrp, task, false);
|
|
||||||
|
|
||||||
out_finish:
|
|
||||||
cgroup_procs_write_finish(task, locked);
|
|
||||||
out_unlock:
|
|
||||||
cgroup_kn_unlock(of->kn);
|
|
||||||
|
|
||||||
return ret ?: nbytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cgroup core interface files for the default hierarchy */
|
/* cgroup core interface files for the default hierarchy */
|
||||||
|
Reference in New Issue
Block a user