1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28
29
30
31
32enum freezer_state_flags {
33 CGROUP_FREEZER_ONLINE = (1 << 0),
34 CGROUP_FREEZING_SELF = (1 << 1),
35 CGROUP_FREEZING_PARENT = (1 << 2),
36 CGROUP_FROZEN = (1 << 3),
37
38
39 CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
40};
41
42struct freezer {
43 struct cgroup_subsys_state css;
44 unsigned int state;
45 spinlock_t lock;
46};
47
48static inline struct freezer *cgroup_freezer(struct cgroup *cgroup)
49{
50 return container_of(cgroup_subsys_state(cgroup, freezer_subsys_id),
51 struct freezer, css);
52}
53
54static inline struct freezer *task_freezer(struct task_struct *task)
55{
56 return container_of(task_subsys_state(task, freezer_subsys_id),
57 struct freezer, css);
58}
59
60static struct freezer *parent_freezer(struct freezer *freezer)
61{
62 struct cgroup *pcg = freezer->css.cgroup->parent;
63
64 if (pcg)
65 return cgroup_freezer(pcg);
66 return NULL;
67}
68
69bool cgroup_freezing(struct task_struct *task)
70{
71 bool ret;
72
73 rcu_read_lock();
74 ret = task_freezer(task)->state & CGROUP_FREEZING;
75 rcu_read_unlock();
76
77 return ret;
78}
79
80
81
82
83
84static const char *freezer_state_strs(unsigned int state)
85{
86 if (state & CGROUP_FROZEN)
87 return "FROZEN";
88 if (state & CGROUP_FREEZING)
89 return "FREEZING";
90 return "THAWED";
91};
92
93struct cgroup_subsys freezer_subsys;
94
95static struct cgroup_subsys_state *freezer_css_alloc(struct cgroup *cgroup)
96{
97 struct freezer *freezer;
98
99 freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
100 if (!freezer)
101 return ERR_PTR(-ENOMEM);
102
103 spin_lock_init(&freezer->lock);
104 return &freezer->css;
105}
106
107
108
109
110
111
112
113
114
115static int freezer_css_online(struct cgroup *cgroup)
116{
117 struct freezer *freezer = cgroup_freezer(cgroup);
118 struct freezer *parent = parent_freezer(freezer);
119
120
121
122
123
124
125 if (parent)
126 spin_lock_irq(&parent->lock);
127 spin_lock_nested(&freezer->lock, SINGLE_DEPTH_NESTING);
128
129 freezer->state |= CGROUP_FREEZER_ONLINE;
130
131 if (parent && (parent->state & CGROUP_FREEZING)) {
132 freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
133 atomic_inc(&system_freezing_cnt);
134 }
135
136 spin_unlock(&freezer->lock);
137 if (parent)
138 spin_unlock_irq(&parent->lock);
139
140 return 0;
141}
142
143
144
145
146
147
148
149
150static void freezer_css_offline(struct cgroup *cgroup)
151{
152 struct freezer *freezer = cgroup_freezer(cgroup);
153
154 spin_lock_irq(&freezer->lock);
155
156 if (freezer->state & CGROUP_FREEZING)
157 atomic_dec(&system_freezing_cnt);
158
159 freezer->state = 0;
160
161 spin_unlock_irq(&freezer->lock);
162}
163
164static void freezer_css_free(struct cgroup *cgroup)
165{
166 kfree(cgroup_freezer(cgroup));
167}
168
169
170
171
172
173
174
175
176
177
178static void freezer_attach(struct cgroup *new_cgrp, struct cgroup_taskset *tset)
179{
180 struct freezer *freezer = cgroup_freezer(new_cgrp);
181 struct task_struct *task;
182 bool clear_frozen = false;
183
184 spin_lock_irq(&freezer->lock);
185
186
187
188
189
190
191
192
193
194
195
196 cgroup_taskset_for_each(task, new_cgrp, tset) {
197 if (!(freezer->state & CGROUP_FREEZING)) {
198 __thaw_task(task);
199 } else {
200 freeze_task(task);
201 freezer->state &= ~CGROUP_FROZEN;
202 clear_frozen = true;
203 }
204 }
205
206 spin_unlock_irq(&freezer->lock);
207
208
209
210
211
212
213
214
215 while (clear_frozen && (freezer = parent_freezer(freezer))) {
216 spin_lock_irq(&freezer->lock);
217 freezer->state &= ~CGROUP_FROZEN;
218 clear_frozen = freezer->state & CGROUP_FREEZING;
219 spin_unlock_irq(&freezer->lock);
220 }
221}
222
223static void freezer_fork(struct task_struct *task, void *private)
224{
225 struct freezer *freezer;
226
227 rcu_read_lock();
228 freezer = task_freezer(task);
229
230
231
232
233
234 if (!freezer->css.cgroup->parent)
235 goto out;
236
237 spin_lock_irq(&freezer->lock);
238 if (freezer->state & CGROUP_FREEZING)
239 freeze_task(task);
240 spin_unlock_irq(&freezer->lock);
241out:
242 rcu_read_unlock();
243}
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261static void update_if_frozen(struct cgroup *cgroup)
262{
263 struct freezer *freezer = cgroup_freezer(cgroup);
264 struct cgroup *pos;
265 struct cgroup_iter it;
266 struct task_struct *task;
267
268 WARN_ON_ONCE(!rcu_read_lock_held());
269
270 spin_lock_irq(&freezer->lock);
271
272 if (!(freezer->state & CGROUP_FREEZING) ||
273 (freezer->state & CGROUP_FROZEN))
274 goto out_unlock;
275
276
277 cgroup_for_each_child(pos, cgroup) {
278 struct freezer *child = cgroup_freezer(pos);
279
280 if ((child->state & CGROUP_FREEZER_ONLINE) &&
281 !(child->state & CGROUP_FROZEN))
282 goto out_unlock;
283 }
284
285
286 cgroup_iter_start(cgroup, &it);
287
288 while ((task = cgroup_iter_next(cgroup, &it))) {
289 if (freezing(task)) {
290
291
292
293
294
295
296 if (!frozen(task) && !freezer_should_skip(task))
297 goto out_iter_end;
298 }
299 }
300
301 freezer->state |= CGROUP_FROZEN;
302out_iter_end:
303 cgroup_iter_end(cgroup, &it);
304out_unlock:
305 spin_unlock_irq(&freezer->lock);
306}
307
308static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
309 struct seq_file *m)
310{
311 struct cgroup *pos;
312
313 rcu_read_lock();
314
315
316 cgroup_for_each_descendant_post(pos, cgroup)
317 update_if_frozen(pos);
318 update_if_frozen(cgroup);
319
320 rcu_read_unlock();
321
322 seq_puts(m, freezer_state_strs(cgroup_freezer(cgroup)->state));
323 seq_putc(m, '\n');
324 return 0;
325}
326
327static void freeze_cgroup(struct freezer *freezer)
328{
329 struct cgroup *cgroup = freezer->css.cgroup;
330 struct cgroup_iter it;
331 struct task_struct *task;
332
333 cgroup_iter_start(cgroup, &it);
334 while ((task = cgroup_iter_next(cgroup, &it)))
335 freeze_task(task);
336 cgroup_iter_end(cgroup, &it);
337}
338
339static void unfreeze_cgroup(struct freezer *freezer)
340{
341 struct cgroup *cgroup = freezer->css.cgroup;
342 struct cgroup_iter it;
343 struct task_struct *task;
344
345 cgroup_iter_start(cgroup, &it);
346 while ((task = cgroup_iter_next(cgroup, &it)))
347 __thaw_task(task);
348 cgroup_iter_end(cgroup, &it);
349}
350
351
352
353
354
355
356
357
358
359
360static void freezer_apply_state(struct freezer *freezer, bool freeze,
361 unsigned int state)
362{
363
364 lockdep_assert_held(&freezer->lock);
365
366 if (!(freezer->state & CGROUP_FREEZER_ONLINE))
367 return;
368
369 if (freeze) {
370 if (!(freezer->state & CGROUP_FREEZING))
371 atomic_inc(&system_freezing_cnt);
372 freezer->state |= state;
373 freeze_cgroup(freezer);
374 } else {
375 bool was_freezing = freezer->state & CGROUP_FREEZING;
376
377 freezer->state &= ~state;
378
379 if (!(freezer->state & CGROUP_FREEZING)) {
380 if (was_freezing)
381 atomic_dec(&system_freezing_cnt);
382 freezer->state &= ~CGROUP_FROZEN;
383 unfreeze_cgroup(freezer);
384 }
385 }
386}
387
388
389
390
391
392
393
394
395
396static void freezer_change_state(struct freezer *freezer, bool freeze)
397{
398 struct cgroup *pos;
399
400
401 spin_lock_irq(&freezer->lock);
402 freezer_apply_state(freezer, freeze, CGROUP_FREEZING_SELF);
403 spin_unlock_irq(&freezer->lock);
404
405
406
407
408
409
410 rcu_read_lock();
411 cgroup_for_each_descendant_pre(pos, freezer->css.cgroup) {
412 struct freezer *pos_f = cgroup_freezer(pos);
413 struct freezer *parent = parent_freezer(pos_f);
414
415
416
417
418
419
420 spin_lock_irq(&pos_f->lock);
421 freezer_apply_state(pos_f, parent->state & CGROUP_FREEZING,
422 CGROUP_FREEZING_PARENT);
423 spin_unlock_irq(&pos_f->lock);
424 }
425 rcu_read_unlock();
426}
427
428static int freezer_write(struct cgroup *cgroup, struct cftype *cft,
429 const char *buffer)
430{
431 bool freeze;
432
433 if (strcmp(buffer, freezer_state_strs(0)) == 0)
434 freeze = false;
435 else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
436 freeze = true;
437 else
438 return -EINVAL;
439
440 freezer_change_state(cgroup_freezer(cgroup), freeze);
441 return 0;
442}
443
444static u64 freezer_self_freezing_read(struct cgroup *cgroup, struct cftype *cft)
445{
446 struct freezer *freezer = cgroup_freezer(cgroup);
447
448 return (bool)(freezer->state & CGROUP_FREEZING_SELF);
449}
450
451static u64 freezer_parent_freezing_read(struct cgroup *cgroup, struct cftype *cft)
452{
453 struct freezer *freezer = cgroup_freezer(cgroup);
454
455 return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
456}
457
458static struct cftype files[] = {
459 {
460 .name = "state",
461 .flags = CFTYPE_NOT_ON_ROOT,
462 .read_seq_string = freezer_read,
463 .write_string = freezer_write,
464 },
465 {
466 .name = "self_freezing",
467 .flags = CFTYPE_NOT_ON_ROOT,
468 .read_u64 = freezer_self_freezing_read,
469 },
470 {
471 .name = "parent_freezing",
472 .flags = CFTYPE_NOT_ON_ROOT,
473 .read_u64 = freezer_parent_freezing_read,
474 },
475 { }
476};
477
478struct cgroup_subsys freezer_subsys = {
479 .name = "freezer",
480 .css_alloc = freezer_css_alloc,
481 .css_online = freezer_css_online,
482 .css_offline = freezer_css_offline,
483 .css_free = freezer_css_free,
484 .subsys_id = freezer_subsys_id,
485 .attach = freezer_attach,
486 .fork = freezer_fork,
487 .base_cftypes = files,
488};
489