xen-pciback: Cleanup up pcistub_put_pci_dev
[linux-2.6-microblaze.git] / kernel / cgroup_freezer.c
1 /*
2  * cgroup_freezer.c -  control group freezer subsystem
3  *
4  * Copyright IBM Corporation, 2007
5  *
6  * Author : Cedric Le Goater <clg@fr.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2.1 of the GNU Lesser General Public License
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it would be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  */
16
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/cgroup.h>
20 #include <linux/fs.h>
21 #include <linux/uaccess.h>
22 #include <linux/freezer.h>
23 #include <linux/seq_file.h>
24
25 /*
26  * A cgroup is freezing if any FREEZING flags are set.  FREEZING_SELF is
27  * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
28  * for "THAWED".  FREEZING_PARENT is set if the parent freezer is FREEZING
29  * for whatever reason.  IOW, a cgroup has FREEZING_PARENT set if one of
30  * its ancestors has FREEZING_SELF set.
31  */
32 enum freezer_state_flags {
33         CGROUP_FREEZER_ONLINE   = (1 << 0), /* freezer is fully online */
34         CGROUP_FREEZING_SELF    = (1 << 1), /* this freezer is freezing */
35         CGROUP_FREEZING_PARENT  = (1 << 2), /* the parent freezer is freezing */
36         CGROUP_FROZEN           = (1 << 3), /* this and its descendants frozen */
37
38         /* mask for all FREEZING flags */
39         CGROUP_FREEZING         = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
40 };
41
42 struct freezer {
43         struct cgroup_subsys_state      css;
44         unsigned int                    state;
45         spinlock_t                      lock;
46 };
47
48 static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
49 {
50         return css ? container_of(css, struct freezer, css) : NULL;
51 }
52
53 static inline struct freezer *task_freezer(struct task_struct *task)
54 {
55         return css_freezer(task_css(task, freezer_cgrp_id));
56 }
57
58 static struct freezer *parent_freezer(struct freezer *freezer)
59 {
60         return css_freezer(css_parent(&freezer->css));
61 }
62
63 bool cgroup_freezing(struct task_struct *task)
64 {
65         bool ret;
66
67         rcu_read_lock();
68         ret = task_freezer(task)->state & CGROUP_FREEZING;
69         rcu_read_unlock();
70
71         return ret;
72 }
73
74 /*
75  * cgroups_write_string() limits the size of freezer state strings to
76  * CGROUP_LOCAL_BUFFER_SIZE
77  */
78 static const char *freezer_state_strs(unsigned int state)
79 {
80         if (state & CGROUP_FROZEN)
81                 return "FROZEN";
82         if (state & CGROUP_FREEZING)
83                 return "FREEZING";
84         return "THAWED";
85 };
86
87 static struct cgroup_subsys_state *
88 freezer_css_alloc(struct cgroup_subsys_state *parent_css)
89 {
90         struct freezer *freezer;
91
92         freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
93         if (!freezer)
94                 return ERR_PTR(-ENOMEM);
95
96         spin_lock_init(&freezer->lock);
97         return &freezer->css;
98 }
99
100 /**
101  * freezer_css_online - commit creation of a freezer css
102  * @css: css being created
103  *
104  * We're committing to creation of @css.  Mark it online and inherit
105  * parent's freezing state while holding both parent's and our
106  * freezer->lock.
107  */
108 static int freezer_css_online(struct cgroup_subsys_state *css)
109 {
110         struct freezer *freezer = css_freezer(css);
111         struct freezer *parent = parent_freezer(freezer);
112
113         /*
114          * The following double locking and freezing state inheritance
115          * guarantee that @cgroup can never escape ancestors' freezing
116          * states.  See css_for_each_descendant_pre() for details.
117          */
118         if (parent)
119                 spin_lock_irq(&parent->lock);
120         spin_lock_nested(&freezer->lock, SINGLE_DEPTH_NESTING);
121
122         freezer->state |= CGROUP_FREEZER_ONLINE;
123
124         if (parent && (parent->state & CGROUP_FREEZING)) {
125                 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
126                 atomic_inc(&system_freezing_cnt);
127         }
128
129         spin_unlock(&freezer->lock);
130         if (parent)
131                 spin_unlock_irq(&parent->lock);
132
133         return 0;
134 }
135
136 /**
137  * freezer_css_offline - initiate destruction of a freezer css
138  * @css: css being destroyed
139  *
140  * @css is going away.  Mark it dead and decrement system_freezing_count if
141  * it was holding one.
142  */
143 static void freezer_css_offline(struct cgroup_subsys_state *css)
144 {
145         struct freezer *freezer = css_freezer(css);
146
147         spin_lock_irq(&freezer->lock);
148
149         if (freezer->state & CGROUP_FREEZING)
150                 atomic_dec(&system_freezing_cnt);
151
152         freezer->state = 0;
153
154         spin_unlock_irq(&freezer->lock);
155 }
156
157 static void freezer_css_free(struct cgroup_subsys_state *css)
158 {
159         kfree(css_freezer(css));
160 }
161
162 /*
163  * Tasks can be migrated into a different freezer anytime regardless of its
164  * current state.  freezer_attach() is responsible for making new tasks
165  * conform to the current state.
166  *
167  * Freezer state changes and task migration are synchronized via
168  * @freezer->lock.  freezer_attach() makes the new tasks conform to the
169  * current state and all following state changes can see the new tasks.
170  */
171 static void freezer_attach(struct cgroup_subsys_state *new_css,
172                            struct cgroup_taskset *tset)
173 {
174         struct freezer *freezer = css_freezer(new_css);
175         struct task_struct *task;
176         bool clear_frozen = false;
177
178         spin_lock_irq(&freezer->lock);
179
180         /*
181          * Make the new tasks conform to the current state of @new_css.
182          * For simplicity, when migrating any task to a FROZEN cgroup, we
183          * revert it to FREEZING and let update_if_frozen() determine the
184          * correct state later.
185          *
186          * Tasks in @tset are on @new_css but may not conform to its
187          * current state before executing the following - !frozen tasks may
188          * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
189          */
190         cgroup_taskset_for_each(task, tset) {
191                 if (!(freezer->state & CGROUP_FREEZING)) {
192                         __thaw_task(task);
193                 } else {
194                         freeze_task(task);
195                         freezer->state &= ~CGROUP_FROZEN;
196                         clear_frozen = true;
197                 }
198         }
199
200         spin_unlock_irq(&freezer->lock);
201
202         /*
203          * Propagate FROZEN clearing upwards.  We may race with
204          * update_if_frozen(), but as long as both work bottom-up, either
205          * update_if_frozen() sees child's FROZEN cleared or we clear the
206          * parent's FROZEN later.  No parent w/ !FROZEN children can be
207          * left FROZEN.
208          */
209         while (clear_frozen && (freezer = parent_freezer(freezer))) {
210                 spin_lock_irq(&freezer->lock);
211                 freezer->state &= ~CGROUP_FROZEN;
212                 clear_frozen = freezer->state & CGROUP_FREEZING;
213                 spin_unlock_irq(&freezer->lock);
214         }
215 }
216
217 /**
218  * freezer_fork - cgroup post fork callback
219  * @task: a task which has just been forked
220  *
221  * @task has just been created and should conform to the current state of
222  * the cgroup_freezer it belongs to.  This function may race against
223  * freezer_attach().  Losing to freezer_attach() means that we don't have
224  * to do anything as freezer_attach() will put @task into the appropriate
225  * state.
226  */
227 static void freezer_fork(struct task_struct *task)
228 {
229         struct freezer *freezer;
230
231         rcu_read_lock();
232         freezer = task_freezer(task);
233
234         /*
235          * The root cgroup is non-freezable, so we can skip locking the
236          * freezer.  This is safe regardless of race with task migration.
237          * If we didn't race or won, skipping is obviously the right thing
238          * to do.  If we lost and root is the new cgroup, noop is still the
239          * right thing to do.
240          */
241         if (!parent_freezer(freezer))
242                 goto out;
243
244         /*
245          * Grab @freezer->lock and freeze @task after verifying @task still
246          * belongs to @freezer and it's freezing.  The former is for the
247          * case where we have raced against task migration and lost and
248          * @task is already in a different cgroup which may not be frozen.
249          * This isn't strictly necessary as freeze_task() is allowed to be
250          * called spuriously but let's do it anyway for, if nothing else,
251          * documentation.
252          */
253         spin_lock_irq(&freezer->lock);
254         if (freezer == task_freezer(task) && (freezer->state & CGROUP_FREEZING))
255                 freeze_task(task);
256         spin_unlock_irq(&freezer->lock);
257 out:
258         rcu_read_unlock();
259 }
260
261 /**
262  * update_if_frozen - update whether a cgroup finished freezing
263  * @css: css of interest
264  *
265  * Once FREEZING is initiated, transition to FROZEN is lazily updated by
266  * calling this function.  If the current state is FREEZING but not FROZEN,
267  * this function checks whether all tasks of this cgroup and the descendant
268  * cgroups finished freezing and, if so, sets FROZEN.
269  *
270  * The caller is responsible for grabbing RCU read lock and calling
271  * update_if_frozen() on all descendants prior to invoking this function.
272  *
273  * Task states and freezer state might disagree while tasks are being
274  * migrated into or out of @css, so we can't verify task states against
275  * @freezer state here.  See freezer_attach() for details.
276  */
277 static void update_if_frozen(struct cgroup_subsys_state *css)
278 {
279         struct freezer *freezer = css_freezer(css);
280         struct cgroup_subsys_state *pos;
281         struct css_task_iter it;
282         struct task_struct *task;
283
284         WARN_ON_ONCE(!rcu_read_lock_held());
285
286         spin_lock_irq(&freezer->lock);
287
288         if (!(freezer->state & CGROUP_FREEZING) ||
289             (freezer->state & CGROUP_FROZEN))
290                 goto out_unlock;
291
292         /* are all (live) children frozen? */
293         css_for_each_child(pos, css) {
294                 struct freezer *child = css_freezer(pos);
295
296                 if ((child->state & CGROUP_FREEZER_ONLINE) &&
297                     !(child->state & CGROUP_FROZEN))
298                         goto out_unlock;
299         }
300
301         /* are all tasks frozen? */
302         css_task_iter_start(css, &it);
303
304         while ((task = css_task_iter_next(&it))) {
305                 if (freezing(task)) {
306                         /*
307                          * freezer_should_skip() indicates that the task
308                          * should be skipped when determining freezing
309                          * completion.  Consider it frozen in addition to
310                          * the usual frozen condition.
311                          */
312                         if (!frozen(task) && !freezer_should_skip(task))
313                                 goto out_iter_end;
314                 }
315         }
316
317         freezer->state |= CGROUP_FROZEN;
318 out_iter_end:
319         css_task_iter_end(&it);
320 out_unlock:
321         spin_unlock_irq(&freezer->lock);
322 }
323
324 static int freezer_read(struct seq_file *m, void *v)
325 {
326         struct cgroup_subsys_state *css = seq_css(m), *pos;
327
328         rcu_read_lock();
329
330         /* update states bottom-up */
331         css_for_each_descendant_post(pos, css)
332                 update_if_frozen(pos);
333
334         rcu_read_unlock();
335
336         seq_puts(m, freezer_state_strs(css_freezer(css)->state));
337         seq_putc(m, '\n');
338         return 0;
339 }
340
341 static void freeze_cgroup(struct freezer *freezer)
342 {
343         struct css_task_iter it;
344         struct task_struct *task;
345
346         css_task_iter_start(&freezer->css, &it);
347         while ((task = css_task_iter_next(&it)))
348                 freeze_task(task);
349         css_task_iter_end(&it);
350 }
351
352 static void unfreeze_cgroup(struct freezer *freezer)
353 {
354         struct css_task_iter it;
355         struct task_struct *task;
356
357         css_task_iter_start(&freezer->css, &it);
358         while ((task = css_task_iter_next(&it)))
359                 __thaw_task(task);
360         css_task_iter_end(&it);
361 }
362
363 /**
364  * freezer_apply_state - apply state change to a single cgroup_freezer
365  * @freezer: freezer to apply state change to
366  * @freeze: whether to freeze or unfreeze
367  * @state: CGROUP_FREEZING_* flag to set or clear
368  *
369  * Set or clear @state on @cgroup according to @freeze, and perform
370  * freezing or thawing as necessary.
371  */
372 static void freezer_apply_state(struct freezer *freezer, bool freeze,
373                                 unsigned int state)
374 {
375         /* also synchronizes against task migration, see freezer_attach() */
376         lockdep_assert_held(&freezer->lock);
377
378         if (!(freezer->state & CGROUP_FREEZER_ONLINE))
379                 return;
380
381         if (freeze) {
382                 if (!(freezer->state & CGROUP_FREEZING))
383                         atomic_inc(&system_freezing_cnt);
384                 freezer->state |= state;
385                 freeze_cgroup(freezer);
386         } else {
387                 bool was_freezing = freezer->state & CGROUP_FREEZING;
388
389                 freezer->state &= ~state;
390
391                 if (!(freezer->state & CGROUP_FREEZING)) {
392                         if (was_freezing)
393                                 atomic_dec(&system_freezing_cnt);
394                         freezer->state &= ~CGROUP_FROZEN;
395                         unfreeze_cgroup(freezer);
396                 }
397         }
398 }
399
400 /**
401  * freezer_change_state - change the freezing state of a cgroup_freezer
402  * @freezer: freezer of interest
403  * @freeze: whether to freeze or thaw
404  *
405  * Freeze or thaw @freezer according to @freeze.  The operations are
406  * recursive - all descendants of @freezer will be affected.
407  */
408 static void freezer_change_state(struct freezer *freezer, bool freeze)
409 {
410         struct cgroup_subsys_state *pos;
411
412         /*
413          * Update all its descendants in pre-order traversal.  Each
414          * descendant will try to inherit its parent's FREEZING state as
415          * CGROUP_FREEZING_PARENT.
416          */
417         rcu_read_lock();
418         css_for_each_descendant_pre(pos, &freezer->css) {
419                 struct freezer *pos_f = css_freezer(pos);
420                 struct freezer *parent = parent_freezer(pos_f);
421
422                 spin_lock_irq(&pos_f->lock);
423
424                 if (pos_f == freezer) {
425                         freezer_apply_state(pos_f, freeze,
426                                             CGROUP_FREEZING_SELF);
427                 } else {
428                         /*
429                          * Our update to @parent->state is already visible
430                          * which is all we need.  No need to lock @parent.
431                          * For more info on synchronization, see
432                          * freezer_post_create().
433                          */
434                         freezer_apply_state(pos_f,
435                                             parent->state & CGROUP_FREEZING,
436                                             CGROUP_FREEZING_PARENT);
437                 }
438
439                 spin_unlock_irq(&pos_f->lock);
440         }
441         rcu_read_unlock();
442 }
443
444 static int freezer_write(struct cgroup_subsys_state *css, struct cftype *cft,
445                          char *buffer)
446 {
447         bool freeze;
448
449         if (strcmp(buffer, freezer_state_strs(0)) == 0)
450                 freeze = false;
451         else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
452                 freeze = true;
453         else
454                 return -EINVAL;
455
456         freezer_change_state(css_freezer(css), freeze);
457         return 0;
458 }
459
460 static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
461                                       struct cftype *cft)
462 {
463         struct freezer *freezer = css_freezer(css);
464
465         return (bool)(freezer->state & CGROUP_FREEZING_SELF);
466 }
467
468 static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
469                                         struct cftype *cft)
470 {
471         struct freezer *freezer = css_freezer(css);
472
473         return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
474 }
475
476 static struct cftype files[] = {
477         {
478                 .name = "state",
479                 .flags = CFTYPE_NOT_ON_ROOT,
480                 .seq_show = freezer_read,
481                 .write_string = freezer_write,
482         },
483         {
484                 .name = "self_freezing",
485                 .flags = CFTYPE_NOT_ON_ROOT,
486                 .read_u64 = freezer_self_freezing_read,
487         },
488         {
489                 .name = "parent_freezing",
490                 .flags = CFTYPE_NOT_ON_ROOT,
491                 .read_u64 = freezer_parent_freezing_read,
492         },
493         { }     /* terminate */
494 };
495
496 struct cgroup_subsys freezer_cgrp_subsys = {
497         .css_alloc      = freezer_css_alloc,
498         .css_online     = freezer_css_online,
499         .css_offline    = freezer_css_offline,
500         .css_free       = freezer_css_free,
501         .attach         = freezer_attach,
502         .fork           = freezer_fork,
503         .base_cftypes   = files,
504 };