Skip to content

: CGroup Files: Clean up locking and boilerplate

[email protected] containers lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest8 patches 34 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8: CGroup Files: Add locking mode to cgroups control filesinclude/linux/cgroup.h kernel/cgroup.c+212 -5621 replies
2/8: CGroup Files: Add a cgroup write_string control file methodinclude/linux/cgroup.h kernel/cgroup.c+14 -16 replies
3/8: CGroup Files: Move the release_agent file to use typed handlerskernel/cgroup.c+35 -684 replies
4/8: CGroup Files: Move notify_on_release file to separate write handlerkernel/cgroup.c+13 -80 replies
5/8: CGroup Files: Turn attach_task_by_pid directly into a cgroup write handlerkernel/cgroup.c+3 -640 replies
6/8: CGroup Files: Remove cpuset_common_file_write()kernel/cpuset.c+10 -633 replies
7/8: CGroup Files: Convert devcgroup_access_write() into a cgroup write_string() handlersecurity/device_cgroup.c+22 -570 replies
8/8: CGroup Files: Convert res_counter_write() to be a cgroups write_string() handlerinclude/linux/res_counter.h kernel/res_counter.c mm/memcontrol.c+16 -350 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC/PATCH 0/8]: CGroup Files: Clean up locking and boilerplate

Most of the remaining cgroup control files that implement raw file handlers (those where the userspace pointer/length/ppos are passed through unchanged by cgroups) appear to do so in order to have some control over the kind of locking that their handler uses. In particular, some handlers need to call cgroup_lock() after copying data from userspace and others don't. They also often dynamically allocate memory when it's not strictly needed.

This patchset provides three main features:

1) A new "lockmode" field that indicates what kind of stability/locking guarantees the handler needs from cgroups while it is running. This can be used to reduce the dependency on cgroup_lock(), and prevent its use as a BKL from becoming a contention point as the number of cgroups subsystems grow.

An alternative to this would be to not have cgroups do the locking internally, but to export similar functionality via functions such as

cgroup_lock_rmdir(struct cgroup*)
cgroup_lock_hierarchy(struct cgroup*)
cgroup_lock_attach(struct cgroup*)

that would perform any necessary locking, and recheck !cgroup_removed(). This results in clearer code from the locking point of view, as you can see within the function exactly what locking it relies upon. The downside is that it makes the code more complex in that you can't simply return from the middle of the function, but instead need to save the return code and goto the point in the function that releases the lock. E.g. the difference would be:

int some_handler(struct cgroup *cgrp, struct cftype *cft)

{
	if (!do_locked_operation1(cgrp))
		return -EINVAL;
	return do_locked_operation2(cgrp);
}

(and setting CFT_LOCK_RMDIR in the cftype descriptor)

versus

int some_handler(struct cgroup *cgrp, struct cftype *cft)

{
	int retval = cgroup_lock_rmdir(cgrp);
	if (retval)
		return retval;
	if (!do_locked_operation1(cgrp)) {
		retval = -EINVAL;
		goto out_unlock;
	}
	retval = do_locked_operation2(cgrp);
out_unlock:
	cgroup_unlock_rmdir(cgrp);
	return retval;
}

The latter style is common in the kernel, but my feeling is that the former style is easier to code and to comprehend. I'm interested in what others think.

2) A new "write_string" method which copies the user's data to kernel space and ensures it's nul-terminated, performs any necessary locking/checks, and invokes the handler with the kernelspace buffer

3) Conversion of several raw read/write handlers in cgroup, cpuset, devcgroup and res_counter to use typed handlers and the new locking specifications.

--