cgroup_freezer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/module.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. static inline int __cgroup_freezing_or_frozen(struct task_struct *task)
  46. {
  47. enum freezer_state state = task_freezer(task)->state;
  48. return (state == CGROUP_FREEZING) || (state == CGROUP_FROZEN);
  49. }
  50. int cgroup_freezing_or_frozen(struct task_struct *task)
  51. {
  52. int result;
  53. task_lock(task);
  54. result = __cgroup_freezing_or_frozen(task);
  55. task_unlock(task);
  56. return result;
  57. }
  58. /*
  59. * cgroups_write_string() limits the size of freezer state strings to
  60. * CGROUP_LOCAL_BUFFER_SIZE
  61. */
  62. static const char *freezer_state_strs[] = {
  63. "THAWED",
  64. "FREEZING",
  65. "FROZEN",
  66. };
  67. /*
  68. * State diagram
  69. * Transitions are caused by userspace writes to the freezer.state file.
  70. * The values in parenthesis are state labels. The rest are edge labels.
  71. *
  72. * (THAWED) --FROZEN--> (FREEZING) --FROZEN--> (FROZEN)
  73. * ^ ^ | |
  74. * | \_______THAWED_______/ |
  75. * \__________________________THAWED____________/
  76. */
  77. struct cgroup_subsys freezer_subsys;
  78. /* Locks taken and their ordering
  79. * ------------------------------
  80. * cgroup_mutex (AKA cgroup_lock)
  81. * freezer->lock
  82. * css_set_lock
  83. * task->alloc_lock (AKA task_lock)
  84. * task->sighand->siglock
  85. *
  86. * cgroup code forces css_set_lock to be taken before task->alloc_lock
  87. *
  88. * freezer_create(), freezer_destroy():
  89. * cgroup_mutex [ by cgroup core ]
  90. *
  91. * freezer_can_attach():
  92. * cgroup_mutex (held by caller of can_attach)
  93. *
  94. * cgroup_freezing_or_frozen():
  95. * task->alloc_lock (to get task's cgroup)
  96. *
  97. * freezer_fork() (preserving fork() performance means can't take cgroup_mutex):
  98. * freezer->lock
  99. * sighand->siglock (if the cgroup is freezing)
  100. *
  101. * freezer_read():
  102. * cgroup_mutex
  103. * freezer->lock
  104. * write_lock css_set_lock (cgroup iterator start)
  105. * task->alloc_lock
  106. * read_lock css_set_lock (cgroup iterator start)
  107. *
  108. * freezer_write() (freeze):
  109. * cgroup_mutex
  110. * freezer->lock
  111. * write_lock css_set_lock (cgroup iterator start)
  112. * task->alloc_lock
  113. * read_lock css_set_lock (cgroup iterator start)
  114. * sighand->siglock (fake signal delivery inside freeze_task())
  115. *
  116. * freezer_write() (unfreeze):
  117. * cgroup_mutex
  118. * freezer->lock
  119. * write_lock css_set_lock (cgroup iterator start)
  120. * task->alloc_lock
  121. * read_lock css_set_lock (cgroup iterator start)
  122. * task->alloc_lock (inside thaw_process(), prevents race with refrigerator())
  123. * sighand->siglock
  124. */
  125. static struct cgroup_subsys_state *freezer_create(struct cgroup_subsys *ss,
  126. struct cgroup *cgroup)
  127. {
  128. struct freezer *freezer;
  129. freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
  130. if (!freezer)
  131. return ERR_PTR(-ENOMEM);
  132. spin_lock_init(&freezer->lock);
  133. freezer->state = CGROUP_THAWED;
  134. return &freezer->css;
  135. }
  136. static void freezer_destroy(struct cgroup_subsys *ss,
  137. struct cgroup *cgroup)
  138. {
  139. kfree(cgroup_freezer(cgroup));
  140. }
  141. /* task is frozen or will freeze immediately when next it gets woken */
  142. static bool is_task_frozen_enough(struct task_struct *task)
  143. {
  144. return frozen(task) ||
  145. (task_is_stopped_or_traced(task) && freezing(task));
  146. }
  147. /*
  148. * The call to cgroup_lock() in the freezer.state write method prevents
  149. * a write to that file racing against an attach, and hence the
  150. * can_attach() result will remain valid until the attach completes.
  151. */
  152. static int freezer_can_attach(struct cgroup_subsys *ss,
  153. struct cgroup *new_cgroup,
  154. struct task_struct *task)
  155. {
  156. struct freezer *freezer;
  157. /*
  158. * Anything frozen can't move or be moved to/from.
  159. */
  160. freezer = cgroup_freezer(new_cgroup);
  161. if (freezer->state != CGROUP_THAWED)
  162. return -EBUSY;
  163. return 0;
  164. }
  165. static int freezer_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  166. {
  167. rcu_read_lock();
  168. if (__cgroup_freezing_or_frozen(tsk)) {
  169. rcu_read_unlock();
  170. return -EBUSY;
  171. }
  172. rcu_read_unlock();
  173. return 0;
  174. }
  175. static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
  176. {
  177. struct freezer *freezer;
  178. /*
  179. * No lock is needed, since the task isn't on tasklist yet,
  180. * so it can't be moved to another cgroup, which means the
  181. * freezer won't be removed and will be valid during this
  182. * function call. Nevertheless, apply RCU read-side critical
  183. * section to suppress RCU lockdep false positives.
  184. */
  185. rcu_read_lock();
  186. freezer = task_freezer(task);
  187. rcu_read_unlock();
  188. /*
  189. * The root cgroup is non-freezable, so we can skip the
  190. * following check.
  191. */
  192. if (!freezer->css.cgroup->parent)
  193. return;
  194. spin_lock_irq(&freezer->lock);
  195. BUG_ON(freezer->state == CGROUP_FROZEN);
  196. /* Locking avoids race with FREEZING -> THAWED transitions. */
  197. if (freezer->state == CGROUP_FREEZING)
  198. freeze_task(task, true);
  199. spin_unlock_irq(&freezer->lock);
  200. }
  201. /*
  202. * caller must hold freezer->lock
  203. */
  204. static void update_if_frozen(struct cgroup *cgroup,
  205. struct freezer *freezer)
  206. {
  207. struct cgroup_iter it;
  208. struct task_struct *task;
  209. unsigned int nfrozen = 0, ntotal = 0;
  210. enum freezer_state old_state = freezer->state;
  211. cgroup_iter_start(cgroup, &it);
  212. while ((task = cgroup_iter_next(cgroup, &it))) {
  213. ntotal++;
  214. if (is_task_frozen_enough(task))
  215. nfrozen++;
  216. }
  217. if (old_state == CGROUP_THAWED) {
  218. BUG_ON(nfrozen > 0);
  219. } else if (old_state == CGROUP_FREEZING) {
  220. if (nfrozen == ntotal)
  221. freezer->state = CGROUP_FROZEN;
  222. } else { /* old_state == CGROUP_FROZEN */
  223. BUG_ON(nfrozen != ntotal);
  224. }
  225. cgroup_iter_end(cgroup, &it);
  226. }
  227. static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
  228. struct seq_file *m)
  229. {
  230. struct freezer *freezer;
  231. enum freezer_state state;
  232. if (!cgroup_lock_live_group(cgroup))
  233. return -ENODEV;
  234. freezer = cgroup_freezer(cgroup);
  235. spin_lock_irq(&freezer->lock);
  236. state = freezer->state;
  237. if (state == CGROUP_FREEZING) {
  238. /* We change from FREEZING to FROZEN lazily if the cgroup was
  239. * only partially frozen when we exitted write. */
  240. update_if_frozen(cgroup, freezer);
  241. state = freezer->state;
  242. }
  243. spin_unlock_irq(&freezer->lock);
  244. cgroup_unlock();
  245. seq_puts(m, freezer_state_strs[state]);
  246. seq_putc(m, '\n');
  247. return 0;
  248. }
  249. static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
  250. {
  251. struct cgroup_iter it;
  252. struct task_struct *task;
  253. unsigned int num_cant_freeze_now = 0;
  254. freezer->state = CGROUP_FREEZING;
  255. cgroup_iter_start(cgroup, &it);
  256. while ((task = cgroup_iter_next(cgroup, &it))) {
  257. if (!freeze_task(task, true))
  258. continue;
  259. if (is_task_frozen_enough(task))
  260. continue;
  261. if (!freezing(task) && !freezer_should_skip(task))
  262. num_cant_freeze_now++;
  263. }
  264. cgroup_iter_end(cgroup, &it);
  265. return num_cant_freeze_now ? -EBUSY : 0;
  266. }
  267. static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
  268. {
  269. struct cgroup_iter it;
  270. struct task_struct *task;
  271. cgroup_iter_start(cgroup, &it);
  272. while ((task = cgroup_iter_next(cgroup, &it))) {
  273. thaw_process(task);
  274. }
  275. cgroup_iter_end(cgroup, &it);
  276. freezer->state = CGROUP_THAWED;
  277. }
  278. static int freezer_change_state(struct cgroup *cgroup,
  279. enum freezer_state goal_state)
  280. {
  281. struct freezer *freezer;
  282. int retval = 0;
  283. freezer = cgroup_freezer(cgroup);
  284. spin_lock_irq(&freezer->lock);
  285. update_if_frozen(cgroup, freezer);
  286. if (goal_state == freezer->state)
  287. goto out;
  288. switch (goal_state) {
  289. case CGROUP_THAWED:
  290. unfreeze_cgroup(cgroup, freezer);
  291. break;
  292. case CGROUP_FROZEN:
  293. retval = try_to_freeze_cgroup(cgroup, freezer);
  294. break;
  295. default:
  296. BUG();
  297. }
  298. out:
  299. spin_unlock_irq(&freezer->lock);
  300. return retval;
  301. }
  302. static int freezer_write(struct cgroup *cgroup,
  303. struct cftype *cft,
  304. const char *buffer)
  305. {
  306. int retval;
  307. enum freezer_state goal_state;
  308. if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
  309. goal_state = CGROUP_THAWED;
  310. else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
  311. goal_state = CGROUP_FROZEN;
  312. else
  313. return -EINVAL;
  314. if (!cgroup_lock_live_group(cgroup))
  315. return -ENODEV;
  316. retval = freezer_change_state(cgroup, goal_state);
  317. cgroup_unlock();
  318. return retval;
  319. }
  320. static struct cftype files[] = {
  321. {
  322. .name = "state",
  323. .read_seq_string = freezer_read,
  324. .write_string = freezer_write,
  325. },
  326. };
  327. static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
  328. {
  329. if (!cgroup->parent)
  330. return 0;
  331. return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
  332. }
  333. struct cgroup_subsys freezer_subsys = {
  334. .name = "freezer",
  335. .create = freezer_create,
  336. .destroy = freezer_destroy,
  337. .populate = freezer_populate,
  338. .subsys_id = freezer_subsys_id,
  339. .can_attach = freezer_can_attach,
  340. .can_attach_task = freezer_can_attach_task,
  341. .pre_attach = NULL,
  342. .attach_task = NULL,
  343. .attach = NULL,
  344. .fork = freezer_fork,
  345. .exit = NULL,
  346. };