]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - kernel/cgroup_freezer.c
8a92b0e52099185136ace406dcefa7129b1ba5e1
[~shefty/rdma-dev.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 enum freezer_state {
26         CGROUP_THAWED = 0,
27         CGROUP_FREEZING,
28         CGROUP_FROZEN,
29 };
30
31 struct freezer {
32         struct cgroup_subsys_state css;
33         enum freezer_state state;
34         spinlock_t lock; /* protects _writes_ to state */
35 };
36
37 static inline struct freezer *cgroup_freezer(
38                 struct cgroup *cgroup)
39 {
40         return container_of(
41                 cgroup_subsys_state(cgroup, freezer_subsys_id),
42                 struct freezer, css);
43 }
44
45 static inline struct freezer *task_freezer(struct task_struct *task)
46 {
47         return container_of(task_subsys_state(task, freezer_subsys_id),
48                             struct freezer, css);
49 }
50
51 bool cgroup_freezing(struct task_struct *task)
52 {
53         enum freezer_state state;
54         bool ret;
55
56         rcu_read_lock();
57         state = task_freezer(task)->state;
58         ret = state == CGROUP_FREEZING || state == CGROUP_FROZEN;
59         rcu_read_unlock();
60
61         return ret;
62 }
63
64 /*
65  * cgroups_write_string() limits the size of freezer state strings to
66  * CGROUP_LOCAL_BUFFER_SIZE
67  */
68 static const char *freezer_state_strs[] = {
69         "THAWED",
70         "FREEZING",
71         "FROZEN",
72 };
73
74 /*
75  * State diagram
76  * Transitions are caused by userspace writes to the freezer.state file.
77  * The values in parenthesis are state labels. The rest are edge labels.
78  *
79  * (THAWED) --FROZEN--> (FREEZING) --FROZEN--> (FROZEN)
80  *    ^ ^                    |                     |
81  *    | \_______THAWED_______/                     |
82  *    \__________________________THAWED____________/
83  */
84
85 struct cgroup_subsys freezer_subsys;
86
87 static struct cgroup_subsys_state *freezer_create(struct cgroup *cgroup)
88 {
89         struct freezer *freezer;
90
91         freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
92         if (!freezer)
93                 return ERR_PTR(-ENOMEM);
94
95         spin_lock_init(&freezer->lock);
96         freezer->state = CGROUP_THAWED;
97         return &freezer->css;
98 }
99
100 static void freezer_destroy(struct cgroup *cgroup)
101 {
102         struct freezer *freezer = cgroup_freezer(cgroup);
103
104         if (freezer->state != CGROUP_THAWED)
105                 atomic_dec(&system_freezing_cnt);
106         kfree(freezer);
107 }
108
109 /*
110  * Tasks can be migrated into a different freezer anytime regardless of its
111  * current state.  freezer_attach() is responsible for making new tasks
112  * conform to the current state.
113  *
114  * Freezer state changes and task migration are synchronized via
115  * @freezer->lock.  freezer_attach() makes the new tasks conform to the
116  * current state and all following state changes can see the new tasks.
117  */
118 static void freezer_attach(struct cgroup *new_cgrp, struct cgroup_taskset *tset)
119 {
120         struct freezer *freezer = cgroup_freezer(new_cgrp);
121         struct task_struct *task;
122
123         spin_lock_irq(&freezer->lock);
124
125         /*
126          * Make the new tasks conform to the current state of @new_cgrp.
127          * For simplicity, when migrating any task to a FROZEN cgroup, we
128          * revert it to FREEZING and let update_if_frozen() determine the
129          * correct state later.
130          *
131          * Tasks in @tset are on @new_cgrp but may not conform to its
132          * current state before executing the following - !frozen tasks may
133          * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
134          * This means that, to determine whether to freeze, one should test
135          * whether the state equals THAWED.
136          */
137         cgroup_taskset_for_each(task, new_cgrp, tset) {
138                 if (freezer->state == CGROUP_THAWED) {
139                         __thaw_task(task);
140                 } else {
141                         freeze_task(task);
142                         freezer->state = CGROUP_FREEZING;
143                 }
144         }
145
146         spin_unlock_irq(&freezer->lock);
147 }
148
149 static void freezer_fork(struct task_struct *task)
150 {
151         struct freezer *freezer;
152
153         rcu_read_lock();
154         freezer = task_freezer(task);
155
156         /*
157          * The root cgroup is non-freezable, so we can skip the
158          * following check.
159          */
160         if (!freezer->css.cgroup->parent)
161                 goto out;
162
163         spin_lock_irq(&freezer->lock);
164         /*
165          * @task might have been just migrated into a FROZEN cgroup.  Test
166          * equality with THAWED.  Read the comment in freezer_attach().
167          */
168         if (freezer->state != CGROUP_THAWED)
169                 freeze_task(task);
170         spin_unlock_irq(&freezer->lock);
171 out:
172         rcu_read_unlock();
173 }
174
175 /*
176  * We change from FREEZING to FROZEN lazily if the cgroup was only
177  * partially frozen when we exitted write.  Caller must hold freezer->lock.
178  *
179  * Task states and freezer state might disagree while tasks are being
180  * migrated into or out of @cgroup, so we can't verify task states against
181  * @freezer state here.  See freezer_attach() for details.
182  */
183 static void update_if_frozen(struct cgroup *cgroup, struct freezer *freezer)
184 {
185         struct cgroup_iter it;
186         struct task_struct *task;
187
188         if (freezer->state != CGROUP_FREEZING)
189                 return;
190
191         cgroup_iter_start(cgroup, &it);
192
193         while ((task = cgroup_iter_next(cgroup, &it))) {
194                 if (freezing(task)) {
195                         /*
196                          * freezer_should_skip() indicates that the task
197                          * should be skipped when determining freezing
198                          * completion.  Consider it frozen in addition to
199                          * the usual frozen condition.
200                          */
201                         if (!frozen(task) && !task_is_stopped_or_traced(task) &&
202                             !freezer_should_skip(task))
203                                 goto notyet;
204                 }
205         }
206
207         freezer->state = CGROUP_FROZEN;
208 notyet:
209         cgroup_iter_end(cgroup, &it);
210 }
211
212 static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
213                         struct seq_file *m)
214 {
215         struct freezer *freezer;
216         enum freezer_state state;
217
218         freezer = cgroup_freezer(cgroup);
219         spin_lock_irq(&freezer->lock);
220         update_if_frozen(cgroup, freezer);
221         state = freezer->state;
222         spin_unlock_irq(&freezer->lock);
223
224         seq_puts(m, freezer_state_strs[state]);
225         seq_putc(m, '\n');
226         return 0;
227 }
228
229 static void freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
230 {
231         struct cgroup_iter it;
232         struct task_struct *task;
233
234         cgroup_iter_start(cgroup, &it);
235         while ((task = cgroup_iter_next(cgroup, &it)))
236                 freeze_task(task);
237         cgroup_iter_end(cgroup, &it);
238 }
239
240 static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
241 {
242         struct cgroup_iter it;
243         struct task_struct *task;
244
245         cgroup_iter_start(cgroup, &it);
246         while ((task = cgroup_iter_next(cgroup, &it)))
247                 __thaw_task(task);
248         cgroup_iter_end(cgroup, &it);
249 }
250
251 static void freezer_change_state(struct cgroup *cgroup,
252                                  enum freezer_state goal_state)
253 {
254         struct freezer *freezer = cgroup_freezer(cgroup);
255
256         /* also synchronizes against task migration, see freezer_attach() */
257         spin_lock_irq(&freezer->lock);
258
259         switch (goal_state) {
260         case CGROUP_THAWED:
261                 if (freezer->state != CGROUP_THAWED)
262                         atomic_dec(&system_freezing_cnt);
263                 freezer->state = CGROUP_THAWED;
264                 unfreeze_cgroup(cgroup, freezer);
265                 break;
266         case CGROUP_FROZEN:
267                 if (freezer->state == CGROUP_THAWED)
268                         atomic_inc(&system_freezing_cnt);
269                 freezer->state = CGROUP_FREEZING;
270                 freeze_cgroup(cgroup, freezer);
271                 break;
272         default:
273                 BUG();
274         }
275
276         spin_unlock_irq(&freezer->lock);
277 }
278
279 static int freezer_write(struct cgroup *cgroup,
280                          struct cftype *cft,
281                          const char *buffer)
282 {
283         enum freezer_state goal_state;
284
285         if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
286                 goal_state = CGROUP_THAWED;
287         else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
288                 goal_state = CGROUP_FROZEN;
289         else
290                 return -EINVAL;
291
292         freezer_change_state(cgroup, goal_state);
293         return 0;
294 }
295
296 static struct cftype files[] = {
297         {
298                 .name = "state",
299                 .flags = CFTYPE_NOT_ON_ROOT,
300                 .read_seq_string = freezer_read,
301                 .write_string = freezer_write,
302         },
303         { }     /* terminate */
304 };
305
306 struct cgroup_subsys freezer_subsys = {
307         .name           = "freezer",
308         .create         = freezer_create,
309         .destroy        = freezer_destroy,
310         .subsys_id      = freezer_subsys_id,
311         .attach         = freezer_attach,
312         .fork           = freezer_fork,
313         .base_cftypes   = files,
314
315         /*
316          * freezer subsys doesn't handle hierarchy at all.  Frozen state
317          * should be inherited through the hierarchy - if a parent is
318          * frozen, all its children should be frozen.  Fix it and remove
319          * the following.
320          */
321         .broken_hierarchy = true,
322 };