cgroup_freezer.c 8.1 KB

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