Re: [PATCH 7/8] cgroups: Add a task counter subsystem

Andrew Morton
2011-08-01T23:14:04Z

On Fri, 29 Jul 2011 18:13:29 +0200
Frederic Weisbecker <fweisbec@gmail.com> wrote:

> Add a new subsystem to limit the number of running tasks,
> similar to the NR_PROC rlimit but in the scope of a cgroup.
>
> This is a step to be able to isolate a bit more a cgroup against
> the rest of the system and limit the global impact of a fork bomb
> inside a given cgroup.
>
> ...
>
> +config CGROUP_TASK_COUNTER
> + bool "Control number of tasks in a cgroup"
> + depends on RESOURCE_COUNTERS
> + help
> + This option let the user to set up an upper bound allowed number
> + of tasks inside a cgroup.

whitespace went weird.

>
> ...
>
+
> +static void task_counter_post_clone(struct cgroup_subsys *ss, struct cgroup *cgrp)
> +{
> + res_counter_inherit(cgroup_task_counter_res(cgrp), RES_LIMIT);

cgroup_task_counter_res() has code in it to carefully return NULL in
one situation, but if it does this, res_counter_inherit() will then
cheerily oops. This makes no sense.

> +}
> +
>
> ...
>
> +/* Protected amongst can_attach_task/attach_task/cancel_attach_task by cgroup mutex */
> +static struct res_counter *common_ancestor;
> +
> +static int task_counter_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
> + struct task_struct *tsk)
> +{
> + struct res_counter *res = cgroup_task_counter_res(cgrp);
> + struct res_counter *old_res = cgroup_task_counter_res(old_cgrp);
> + struct res_counter *limit_fail_at;
> +
> + common_ancestor = res_counter_common_ancestor(res, old_res);

This might oops too?

> + return res_counter_charge_until(res, common_ancestor, 1, &limit_fail_at);
> +}
> +
>
> ...
>
> +int cgroup_task_counter_fork(struct task_struct *child)
> +{
> + struct cgroup_subsys_state *css = child->cgroups->subsys[tasks_subsys_id];
> + struct cgroup *cgrp = css->cgroup;
> + struct res_counter *limit_fail_at;
> +
> + /* Optimize for the root cgroup case, which doesn't have a limit */
> + if (!cgrp->parent)
> + return 0;
> +
> + return res_counter_charge(cgroup_task_counter_res(cgrp), 1, &limit_fail_at);
> +}

It took a while for me to work out the meaning of the return value from
this function. Some documentation would be nice?