cgroup_freezer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /* Locks taken and their ordering
  76. * ------------------------------
  77. * cgroup_mutex (AKA cgroup_lock)
  78. * freezer->lock
  79. * css_set_lock
  80. * task->alloc_lock (AKA task_lock)
  81. * task->sighand->siglock
  82. *
  83. * cgroup code forces css_set_lock to be taken before task->alloc_lock
  84. *
  85. * freezer_create(), freezer_destroy():
  86. * cgroup_mutex [ by cgroup core ]
  87. *
  88. * freezer_can_attach():
  89. * cgroup_mutex (held by caller of can_attach)
  90. *
  91. * freezer_fork() (preserving fork() performance means can't take cgroup_mutex):
  92. * freezer->lock
  93. * sighand->siglock (if the cgroup is freezing)
  94. *
  95. * freezer_read():
  96. * cgroup_mutex
  97. * freezer->lock
  98. * write_lock css_set_lock (cgroup iterator start)
  99. * task->alloc_lock
  100. * read_lock css_set_lock (cgroup iterator start)
  101. *
  102. * freezer_write() (freeze):
  103. * cgroup_mutex
  104. * freezer->lock
  105. * write_lock css_set_lock (cgroup iterator start)
  106. * task->alloc_lock
  107. * read_lock css_set_lock (cgroup iterator start)
  108. * sighand->siglock (fake signal delivery inside freeze_task())
  109. *
  110. * freezer_write() (unfreeze):
  111. * cgroup_mutex
  112. * freezer->lock
  113. * write_lock css_set_lock (cgroup iterator start)
  114. * task->alloc_lock
  115. * read_lock css_set_lock (cgroup iterator start)
  116. * task->alloc_lock (inside __thaw_task(), prevents race with refrigerator())
  117. * sighand->siglock
  118. */
  119. static struct cgroup_subsys_state *freezer_create(struct cgroup *cgroup)
  120. {
  121. struct freezer *freezer;
  122. freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
  123. if (!freezer)
  124. return ERR_PTR(-ENOMEM);
  125. spin_lock_init(&freezer->lock);
  126. freezer->state = CGROUP_THAWED;
  127. return &freezer->css;
  128. }
  129. static void freezer_destroy(struct cgroup *cgroup)
  130. {
  131. struct freezer *freezer = cgroup_freezer(cgroup);
  132. if (freezer->state != CGROUP_THAWED)
  133. atomic_dec(&system_freezing_cnt);
  134. kfree(freezer);
  135. }
  136. /* task is frozen or will freeze immediately when next it gets woken */
  137. static bool is_task_frozen_enough(struct task_struct *task)
  138. {
  139. return frozen(task) ||
  140. (task_is_stopped_or_traced(task) && freezing(task));
  141. }
  142. /*
  143. * The call to cgroup_lock() in the freezer.state write method prevents
  144. * a write to that file racing against an attach, and hence the
  145. * can_attach() result will remain valid until the attach completes.
  146. */
  147. static int freezer_can_attach(struct cgroup *new_cgroup,
  148. struct cgroup_taskset *tset)
  149. {
  150. struct freezer *freezer;
  151. struct task_struct *task;
  152. /*
  153. * Anything frozen can't move or be moved to/from.
  154. */
  155. cgroup_taskset_for_each(task, new_cgroup, tset)
  156. if (cgroup_freezing(task))
  157. return -EBUSY;
  158. freezer = cgroup_freezer(new_cgroup);
  159. if (freezer->state != CGROUP_THAWED)
  160. return -EBUSY;
  161. return 0;
  162. }
  163. static void freezer_fork(struct task_struct *task)
  164. {
  165. struct freezer *freezer;
  166. rcu_read_lock();
  167. freezer = task_freezer(task);
  168. /*
  169. * The root cgroup is non-freezable, so we can skip the
  170. * following check.
  171. */
  172. if (!freezer->css.cgroup->parent)
  173. goto out;
  174. spin_lock_irq(&freezer->lock);
  175. BUG_ON(freezer->state == CGROUP_FROZEN);
  176. /* Locking avoids race with FREEZING -> THAWED transitions. */
  177. if (freezer->state == CGROUP_FREEZING)
  178. freeze_task(task);
  179. spin_unlock_irq(&freezer->lock);
  180. out:
  181. rcu_read_unlock();
  182. }
  183. /*
  184. * caller must hold freezer->lock
  185. */
  186. static void update_if_frozen(struct cgroup *cgroup,
  187. struct freezer *freezer)
  188. {
  189. struct cgroup_iter it;
  190. struct task_struct *task;
  191. unsigned int nfrozen = 0, ntotal = 0;
  192. enum freezer_state old_state = freezer->state;
  193. cgroup_iter_start(cgroup, &it);
  194. while ((task = cgroup_iter_next(cgroup, &it))) {
  195. ntotal++;
  196. if (freezing(task) && is_task_frozen_enough(task))
  197. nfrozen++;
  198. }
  199. if (old_state == CGROUP_THAWED) {
  200. BUG_ON(nfrozen > 0);
  201. } else if (old_state == CGROUP_FREEZING) {
  202. if (nfrozen == ntotal)
  203. freezer->state = CGROUP_FROZEN;
  204. } else { /* old_state == CGROUP_FROZEN */
  205. BUG_ON(nfrozen != ntotal);
  206. }
  207. cgroup_iter_end(cgroup, &it);
  208. }
  209. static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
  210. struct seq_file *m)
  211. {
  212. struct freezer *freezer;
  213. enum freezer_state state;
  214. if (!cgroup_lock_live_group(cgroup))
  215. return -ENODEV;
  216. freezer = cgroup_freezer(cgroup);
  217. spin_lock_irq(&freezer->lock);
  218. state = freezer->state;
  219. if (state == CGROUP_FREEZING) {
  220. /* We change from FREEZING to FROZEN lazily if the cgroup was
  221. * only partially frozen when we exitted write. */
  222. update_if_frozen(cgroup, freezer);
  223. state = freezer->state;
  224. }
  225. spin_unlock_irq(&freezer->lock);
  226. cgroup_unlock();
  227. seq_puts(m, freezer_state_strs[state]);
  228. seq_putc(m, '\n');
  229. return 0;
  230. }
  231. static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
  232. {
  233. struct cgroup_iter it;
  234. struct task_struct *task;
  235. unsigned int num_cant_freeze_now = 0;
  236. cgroup_iter_start(cgroup, &it);
  237. while ((task = cgroup_iter_next(cgroup, &it))) {
  238. if (!freeze_task(task))
  239. continue;
  240. if (is_task_frozen_enough(task))
  241. continue;
  242. if (!freezing(task) && !freezer_should_skip(task))
  243. num_cant_freeze_now++;
  244. }
  245. cgroup_iter_end(cgroup, &it);
  246. return num_cant_freeze_now ? -EBUSY : 0;
  247. }
  248. static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
  249. {
  250. struct cgroup_iter it;
  251. struct task_struct *task;
  252. cgroup_iter_start(cgroup, &it);
  253. while ((task = cgroup_iter_next(cgroup, &it)))
  254. __thaw_task(task);
  255. cgroup_iter_end(cgroup, &it);
  256. }
  257. static int freezer_change_state(struct cgroup *cgroup,
  258. enum freezer_state goal_state)
  259. {
  260. struct freezer *freezer;
  261. int retval = 0;
  262. freezer = cgroup_freezer(cgroup);
  263. spin_lock_irq(&freezer->lock);
  264. update_if_frozen(cgroup, freezer);
  265. switch (goal_state) {
  266. case CGROUP_THAWED:
  267. if (freezer->state != CGROUP_THAWED)
  268. atomic_dec(&system_freezing_cnt);
  269. freezer->state = CGROUP_THAWED;
  270. unfreeze_cgroup(cgroup, freezer);
  271. break;
  272. case CGROUP_FROZEN:
  273. if (freezer->state == CGROUP_THAWED)
  274. atomic_inc(&system_freezing_cnt);
  275. freezer->state = CGROUP_FREEZING;
  276. retval = try_to_freeze_cgroup(cgroup, freezer);
  277. break;
  278. default:
  279. BUG();
  280. }
  281. spin_unlock_irq(&freezer->lock);
  282. return retval;
  283. }
  284. static int freezer_write(struct cgroup *cgroup,
  285. struct cftype *cft,
  286. const char *buffer)
  287. {
  288. int retval;
  289. enum freezer_state goal_state;
  290. if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
  291. goal_state = CGROUP_THAWED;
  292. else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
  293. goal_state = CGROUP_FROZEN;
  294. else
  295. return -EINVAL;
  296. if (!cgroup_lock_live_group(cgroup))
  297. return -ENODEV;
  298. retval = freezer_change_state(cgroup, goal_state);
  299. cgroup_unlock();
  300. return retval;
  301. }
  302. static struct cftype files[] = {
  303. {
  304. .name = "state",
  305. .read_seq_string = freezer_read,
  306. .write_string = freezer_write,
  307. },
  308. };
  309. static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
  310. {
  311. if (!cgroup->parent)
  312. return 0;
  313. return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
  314. }
  315. struct cgroup_subsys freezer_subsys = {
  316. .name = "freezer",
  317. .create = freezer_create,
  318. .destroy = freezer_destroy,
  319. .populate = freezer_populate,
  320. .subsys_id = freezer_subsys_id,
  321. .can_attach = freezer_can_attach,
  322. .fork = freezer_fork,
  323. };