Skip to content

Commit 191a712

Browse files
committed
Merge branch 'for-3.10' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
Pull cgroup updates from Tejun Heo: - Fixes and a lot of cleanups. Locking cleanup is finally complete. cgroup_mutex is no longer exposed to individual controlelrs which used to cause nasty deadlock issues. Li fixed and cleaned up quite a bit including long standing ones like racy cgroup_path(). - device cgroup now supports proper hierarchy thanks to Aristeu. - perf_event cgroup now supports proper hierarchy. - A new mount option "__DEVEL__sane_behavior" is added. As indicated by the name, this option is to be used for development only at this point and generates a warning message when used. Unfortunately, cgroup interface currently has too many brekages and inconsistencies to implement a consistent and unified hierarchy on top. The new flag is used to collect the behavior changes which are necessary to implement consistent unified hierarchy. It's likely that this flag won't be used verbatim when it becomes ready but will be enabled implicitly along with unified hierarchy. The option currently disables some of broken behaviors in cgroup core and also .use_hierarchy switch in memcg (will be routed through -mm), which can be used to make very unusual hierarchy where nesting is partially honored. It will also be used to implement hierarchy support for blk-throttle which would be impossible otherwise without introducing a full separate set of control knobs. This is essentially versioning of interface which isn't very nice but at this point I can't see any other options which would allow keeping the interface the same while moving towards hierarchy behavior which is at least somewhat sane. The planned unified hierarchy is likely to require some level of adaptation from userland anyway, so I think it'd be best to take the chance and update the interface such that it's supportable in the long term. Maintaining the existing interface does complicate cgroup core but shouldn't put too much strain on individual controllers and I think it'd be manageable for the foreseeable future. Maybe we'll be able to drop it in a decade. Fix up conflicts (including a semantic one adding a new #include to ppc that was uncovered by header the file changes) as per Tejun. * 'for-3.10' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup: (45 commits) cpuset: fix compile warning when CONFIG_SMP=n cpuset: fix cpu hotplug vs rebuild_sched_domains() race cpuset: use rebuild_sched_domains() in cpuset_hotplug_workfn() cgroup: restore the call to eventfd->poll() cgroup: fix use-after-free when umounting cgroupfs cgroup: fix broken file xattrs devcg: remove parent_cgroup. memcg: force use_hierarchy if sane_behavior cgroup: remove cgrp->top_cgroup cgroup: introduce sane_behavior mount option move cgroupfs_root to include/linux/cgroup.h cgroup: convert cgroupfs_root flag bits to masks and add CGRP_ prefix cgroup: make cgroup_path() not print double slashes Revert "cgroup: remove bind() method from cgroup_subsys." perf: make perf_event cgroup hierarchical cgroup: implement cgroup_is_descendant() cgroup: make sure parent won't be destroyed before its children cgroup: remove bind() method from cgroup_subsys. devcg: remove broken_hierarchy tag cgroup: remove cgroup_lock_is_held() ...
2 parents 46d9be3 + 2a0010a commit 191a712

12 files changed

Lines changed: 826 additions & 633 deletions

File tree

‎Documentation/cgroups/cgroups.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ You can attach the current shell task by echoing 0:
442442
You can use the cgroup.procs file instead of the tasks file to move all
443443
threads in a threadgroup at once. Echoing the PID of any task in a
444444
threadgroup to cgroup.procs causes all tasks in that threadgroup to be
445-
be attached to the cgroup. Writing 0 to cgroup.procs moves all tasks
445+
attached to the cgroup. Writing 0 to cgroup.procs moves all tasks
446446
in the writing task's threadgroup.
447447

448448
Note: Since every task is always a member of exactly one cgroup in each
@@ -580,6 +580,7 @@ propagation along the hierarchy. See the comment on
580580
cgroup_for_each_descendant_pre() for details.
581581

582582
void css_offline(struct cgroup *cgrp);
583+
(cgroup_mutex held by caller)
583584

584585
This is the counterpart of css_online() and called iff css_online()
585586
has succeeded on @cgrp. This signifies the beginning of the end of

‎Documentation/cgroups/devices.txt‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ either an integer or * for all. Access is a composition of r
1313
The root device cgroup starts with rwm to 'all'. A child device
1414
cgroup gets a copy of the parent. Administrators can then remove
1515
devices from the whitelist or add new entries. A child cgroup can
16-
never receive a device access which is denied by its parent. However
17-
when a device access is removed from a parent it will not also be
18-
removed from the child(ren).
16+
never receive a device access which is denied by its parent.
1917

2018
2. User Interface
2119

@@ -50,3 +48,69 @@ task to a new cgroup. (Again we'll probably want to change that).
5048

5149
A cgroup may not be granted more permissions than the cgroup's
5250
parent has.
51+
52+
4. Hierarchy
53+
54+
device cgroups maintain hierarchy by making sure a cgroup never has more
55+
access permissions than its parent. Every time an entry is written to
56+
a cgroup's devices.deny file, all its children will have that entry removed
57+
from their whitelist and all the locally set whitelist entries will be
58+
re-evaluated. In case one of the locally set whitelist entries would provide
59+
more access than the cgroup's parent, it'll be removed from the whitelist.
60+
61+
Example:
62+
A
63+
/ \
64+
B
65+
66+
group behavior exceptions
67+
A allow "b 8:* rwm", "c 116:1 rw"
68+
B deny "c 1:3 rwm", "c 116:2 rwm", "b 3:* rwm"
69+
70+
If a device is denied in group A:
71+
# echo "c 116:* r" > A/devices.deny
72+
it'll propagate down and after revalidating B's entries, the whitelist entry
73+
"c 116:2 rwm" will be removed:
74+
75+
group whitelist entries denied devices
76+
A all "b 8:* rwm", "c 116:* rw"
77+
B "c 1:3 rwm", "b 3:* rwm" all the rest
78+
79+
In case parent's exceptions change and local exceptions are not allowed
80+
anymore, they'll be deleted.
81+
82+
Notice that new whitelist entries will not be propagated:
83+
A
84+
/ \
85+
B
86+
87+
group whitelist entries denied devices
88+
A "c 1:3 rwm", "c 1:5 r" all the rest
89+
B "c 1:3 rwm", "c 1:5 r" all the rest
90+
91+
when adding "c *:3 rwm":
92+
# echo "c *:3 rwm" >A/devices.allow
93+
94+
the result:
95+
group whitelist entries denied devices
96+
A "c *:3 rwm", "c 1:5 r" all the rest
97+
B "c 1:3 rwm", "c 1:5 r" all the rest
98+
99+
but now it'll be possible to add new entries to B:
100+
# echo "c 2:3 rwm" >B/devices.allow
101+
# echo "c 50:3 r" >B/devices.allow
102+
or even
103+
# echo "c *:3 rwm" >B/devices.allow
104+
105+
Allowing or denying all by writing 'a' to devices.allow or devices.deny will
106+
not be possible once the device cgroups has children.
107+
108+
4.1 Hierarchy (internal implementation)
109+
110+
device cgroups is implemented internally using a behavior (ALLOW, DENY) and a
111+
list of exceptions. The internal state is controlled using the same user
112+
interface to preserve compatibility with the previous whitelist-only
113+
implementation. Removal or addition of exceptions that will reduce the access
114+
to devices will be propagated down the hierarchy.
115+
For every propagated exception, the effective rules will be re-evaluated based
116+
on current parent's access rules.

‎arch/powerpc/mm/numa.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/pfn.h>
2323
#include <linux/cpuset.h>
2424
#include <linux/node.h>
25+
#include <linux/slab.h>
2526
#include <asm/sparsemem.h>
2627
#include <asm/prom.h>
2728
#include <asm/smp.h>

‎block/blk-cgroup.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
247247
{
248248
int ret;
249249

250-
rcu_read_lock();
251250
ret = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
252-
rcu_read_unlock();
253251
if (ret)
254252
strncpy(buf, "<unavailable>", buflen);
255253
return ret;

‎include/linux/cgroup.h‎

Lines changed: 152 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/idr.h>
2020
#include <linux/workqueue.h>
2121
#include <linux/xattr.h>
22+
#include <linux/fs.h>
2223

2324
#ifdef CONFIG_CGROUPS
2425

@@ -30,10 +31,6 @@ struct css_id;
3031

3132
extern int cgroup_init_early(void);
3233
extern int cgroup_init(void);
33-
extern void cgroup_lock(void);
34-
extern int cgroup_lock_is_held(void);
35-
extern bool cgroup_lock_live_group(struct cgroup *cgrp);
36-
extern void cgroup_unlock(void);
3734
extern void cgroup_fork(struct task_struct *p);
3835
extern void cgroup_post_fork(struct task_struct *p);
3936
extern void cgroup_exit(struct task_struct *p, int run_callbacks);
@@ -44,14 +41,25 @@ extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
4441

4542
extern const struct file_operations proc_cgroup_operations;
4643

47-
/* Define the enumeration of all builtin cgroup subsystems */
44+
/*
45+
* Define the enumeration of all cgroup subsystems.
46+
*
47+
* We define ids for builtin subsystems and then modular ones.
48+
*/
4849
#define SUBSYS(_x) _x ## _subsys_id,
49-
#define IS_SUBSYS_ENABLED(option) IS_ENABLED(option)
5050
enum cgroup_subsys_id {
51+
#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
52+
#include <linux/cgroup_subsys.h>
53+
#undef IS_SUBSYS_ENABLED
54+
CGROUP_BUILTIN_SUBSYS_COUNT,
55+
56+
__CGROUP_SUBSYS_TEMP_PLACEHOLDER = CGROUP_BUILTIN_SUBSYS_COUNT - 1,
57+
58+
#define IS_SUBSYS_ENABLED(option) IS_MODULE(option)
5159
#include <linux/cgroup_subsys.h>
60+
#undef IS_SUBSYS_ENABLED
5261
CGROUP_SUBSYS_COUNT,
5362
};
54-
#undef IS_SUBSYS_ENABLED
5563
#undef SUBSYS
5664

5765
/* Per-subsystem/per-cgroup state maintained by the system. */
@@ -148,6 +156,13 @@ enum {
148156
* specified at mount time and thus is implemented here.
149157
*/
150158
CGRP_CPUSET_CLONE_CHILDREN,
159+
/* see the comment above CGRP_ROOT_SANE_BEHAVIOR for details */
160+
CGRP_SANE_BEHAVIOR,
161+
};
162+
163+
struct cgroup_name {
164+
struct rcu_head rcu_head;
165+
char name[];
151166
};
152167

153168
struct cgroup {
@@ -172,11 +187,23 @@ struct cgroup {
172187
struct cgroup *parent; /* my parent */
173188
struct dentry *dentry; /* cgroup fs entry, RCU protected */
174189

190+
/*
191+
* This is a copy of dentry->d_name, and it's needed because
192+
* we can't use dentry->d_name in cgroup_path().
193+
*
194+
* You must acquire rcu_read_lock() to access cgrp->name, and
195+
* the only place that can change it is rename(), which is
196+
* protected by parent dir's i_mutex.
197+
*
198+
* Normally you should use cgroup_name() wrapper rather than
199+
* access it directly.
200+
*/
201+
struct cgroup_name __rcu *name;
202+
175203
/* Private pointers for each registered subsystem */
176204
struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
177205

178206
struct cgroupfs_root *root;
179-
struct cgroup *top_cgroup;
180207

181208
/*
182209
* List of cg_cgroup_links pointing at css_sets with
@@ -213,6 +240,96 @@ struct cgroup {
213240
struct simple_xattrs xattrs;
214241
};
215242

243+
#define MAX_CGROUP_ROOT_NAMELEN 64
244+
245+
/* cgroupfs_root->flags */
246+
enum {
247+
/*
248+
* Unfortunately, cgroup core and various controllers are riddled
249+
* with idiosyncrasies and pointless options. The following flag,
250+
* when set, will force sane behavior - some options are forced on,
251+
* others are disallowed, and some controllers will change their
252+
* hierarchical or other behaviors.
253+
*
254+
* The set of behaviors affected by this flag are still being
255+
* determined and developed and the mount option for this flag is
256+
* prefixed with __DEVEL__. The prefix will be dropped once we
257+
* reach the point where all behaviors are compatible with the
258+
* planned unified hierarchy, which will automatically turn on this
259+
* flag.
260+
*
261+
* The followings are the behaviors currently affected this flag.
262+
*
263+
* - Mount options "noprefix" and "clone_children" are disallowed.
264+
* Also, cgroupfs file cgroup.clone_children is not created.
265+
*
266+
* - When mounting an existing superblock, mount options should
267+
* match.
268+
*
269+
* - Remount is disallowed.
270+
*
271+
* - memcg: use_hierarchy is on by default and the cgroup file for
272+
* the flag is not created.
273+
*
274+
* The followings are planned changes.
275+
*
276+
* - release_agent will be disallowed once replacement notification
277+
* mechanism is implemented.
278+
*/
279+
CGRP_ROOT_SANE_BEHAVIOR = (1 << 0),
280+
281+
CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
282+
CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
283+
};
284+
285+
/*
286+
* A cgroupfs_root represents the root of a cgroup hierarchy, and may be
287+
* associated with a superblock to form an active hierarchy. This is
288+
* internal to cgroup core. Don't access directly from controllers.
289+
*/
290+
struct cgroupfs_root {
291+
struct super_block *sb;
292+
293+
/*
294+
* The bitmask of subsystems intended to be attached to this
295+
* hierarchy
296+
*/
297+
unsigned long subsys_mask;
298+
299+
/* Unique id for this hierarchy. */
300+
int hierarchy_id;
301+
302+
/* The bitmask of subsystems currently attached to this hierarchy */
303+
unsigned long actual_subsys_mask;
304+
305+
/* A list running through the attached subsystems */
306+
struct list_head subsys_list;
307+
308+
/* The root cgroup for this hierarchy */
309+
struct cgroup top_cgroup;
310+
311+
/* Tracks how many cgroups are currently defined in hierarchy.*/
312+
int number_of_cgroups;
313+
314+
/* A list running through the active hierarchies */
315+
struct list_head root_list;
316+
317+
/* All cgroups on this root, cgroup_mutex protected */
318+
struct list_head allcg_list;
319+
320+
/* Hierarchy-specific flags */
321+
unsigned long flags;
322+
323+
/* IDs for cgroups in this hierarchy */
324+
struct ida cgroup_ida;
325+
326+
/* The path to use for release notifications. */
327+
char release_agent_path[PATH_MAX];
328+
329+
/* The name for this hierarchy - may be empty */
330+
char name[MAX_CGROUP_ROOT_NAMELEN];
331+
};
332+
216333
/*
217334
* A css_set is a structure holding pointers to a set of
218335
* cgroup_subsys_state objects. This saves space in the task struct
@@ -278,6 +395,7 @@ struct cgroup_map_cb {
278395
/* cftype->flags */
279396
#define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
280397
#define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create on root cg */
398+
#define CFTYPE_INSANE (1U << 2) /* don't create if sane_behavior */
281399

282400
#define MAX_CFTYPE_NAME 64
283401

@@ -304,9 +422,6 @@ struct cftype {
304422
/* CFTYPE_* flags */
305423
unsigned int flags;
306424

307-
/* file xattrs */
308-
struct simple_xattrs xattrs;
309-
310425
int (*open)(struct inode *inode, struct file *file);
311426
ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
312427
struct file *file,
@@ -404,18 +519,31 @@ struct cgroup_scanner {
404519
void *data;
405520
};
406521

522+
/*
523+
* See the comment above CGRP_ROOT_SANE_BEHAVIOR for details. This
524+
* function can be called as long as @cgrp is accessible.
525+
*/
526+
static inline bool cgroup_sane_behavior(const struct cgroup *cgrp)
527+
{
528+
return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR;
529+
}
530+
531+
/* Caller should hold rcu_read_lock() */
532+
static inline const char *cgroup_name(const struct cgroup *cgrp)
533+
{
534+
return rcu_dereference(cgrp->name)->name;
535+
}
536+
407537
int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
408538
int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
409539

410540
int cgroup_is_removed(const struct cgroup *cgrp);
541+
bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
411542

412543
int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
413544

414545
int cgroup_task_count(const struct cgroup *cgrp);
415546

416-
/* Return true if cgrp is a descendant of the task's cgroup */
417-
int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
418-
419547
/*
420548
* Control Group taskset, used to pass around set of tasks to cgroup_subsys
421549
* methods.
@@ -523,10 +651,16 @@ static inline struct cgroup_subsys_state *cgroup_subsys_state(
523651
* rcu_dereference_check() conditions, such as locks used during the
524652
* cgroup_subsys::attach() methods.
525653
*/
654+
#ifdef CONFIG_PROVE_RCU
655+
extern struct mutex cgroup_mutex;
656+
#define task_subsys_state_check(task, subsys_id, __c) \
657+
rcu_dereference_check((task)->cgroups->subsys[(subsys_id)], \
658+
lockdep_is_held(&(task)->alloc_lock) || \
659+
lockdep_is_held(&cgroup_mutex) || (__c))
660+
#else
526661
#define task_subsys_state_check(task, subsys_id, __c) \
527-
rcu_dereference_check(task->cgroups->subsys[subsys_id], \
528-
lockdep_is_held(&task->alloc_lock) || \
529-
cgroup_lock_is_held() || (__c))
662+
rcu_dereference((task)->cgroups->subsys[(subsys_id)])
663+
#endif
530664

531665
static inline struct cgroup_subsys_state *
532666
task_subsys_state(struct task_struct *task, int subsys_id)
@@ -661,8 +795,8 @@ struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
661795
struct cgroup_iter *it);
662796
void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
663797
int cgroup_scan_tasks(struct cgroup_scanner *scan);
664-
int cgroup_attach_task(struct cgroup *, struct task_struct *);
665798
int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
799+
int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
666800

667801
/*
668802
* CSS ID is ID for cgroup_subsys_state structs under subsys. This only works

‎include/linux/cpuset.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <linux/sched.h>
1212
#include <linux/cpumask.h>
1313
#include <linux/nodemask.h>
14-
#include <linux/cgroup.h>
1514
#include <linux/mm.h>
1615

1716
#ifdef CONFIG_CPUSETS

‎include/linux/res_counter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* info about what this counter is.
1414
*/
1515

16-
#include <linux/cgroup.h>
16+
#include <linux/spinlock.h>
1717
#include <linux/errno.h>
1818

1919
/*

0 commit comments

Comments
 (0)