cgroup_freezer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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, bool threadgroup)
  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. rcu_read_lock();
  164. if (__cgroup_freezing_or_frozen(task)) {
  165. rcu_read_unlock();
  166. return -EBUSY;
  167. }
  168. rcu_read_unlock();
  169. if (threadgroup) {
  170. struct task_struct *c;
  171. rcu_read_lock();
  172. list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
  173. if (__cgroup_freezing_or_frozen(c)) {
  174. rcu_read_unlock();
  175. return -EBUSY;
  176. }
  177. }
  178. rcu_read_unlock();
  179. }
  180. return 0;
  181. }
  182. static void freezer_fork(struct cgroup_subsys *ss, struct task_struct *task)
  183. {
  184. struct freezer *freezer;
  185. /*
  186. * No lock is needed, since the task isn't on tasklist yet,
  187. * so it can't be moved to another cgroup, which means the
  188. * freezer won't be removed and will be valid during this
  189. * function call. Nevertheless, apply RCU read-side critical
  190. * section to suppress RCU lockdep false positives.
  191. */
  192. rcu_read_lock();
  193. freezer = task_freezer(task);
  194. rcu_read_unlock();
  195. /*
  196. * The root cgroup is non-freezable, so we can skip the
  197. * following check.
  198. */
  199. if (!freezer->css.cgroup->parent)
  200. return;
  201. spin_lock_irq(&freezer->lock);
  202. BUG_ON(freezer->state == CGROUP_FROZEN);
  203. /* Locking avoids race with FREEZING -> THAWED transitions. */
  204. if (freezer->state == CGROUP_FREEZING)
  205. freeze_task(task, true);
  206. spin_unlock_irq(&freezer->lock);
  207. }
  208. /*
  209. * caller must hold freezer->lock
  210. */
  211. static void update_freezer_state(struct cgroup *cgroup,
  212. struct freezer *freezer)
  213. {
  214. struct cgroup_iter it;
  215. struct task_struct *task;
  216. unsigned int nfrozen = 0, ntotal = 0;
  217. cgroup_iter_start(cgroup, &it);
  218. while ((task = cgroup_iter_next(cgroup, &it))) {
  219. ntotal++;
  220. if (is_task_frozen_enough(task))
  221. nfrozen++;
  222. }
  223. /*
  224. * Transition to FROZEN when no new tasks can be added ensures
  225. * that we never exist in the FROZEN state while there are unfrozen
  226. * tasks.
  227. */
  228. if (nfrozen == ntotal)
  229. freezer->state = CGROUP_FROZEN;
  230. else if (nfrozen > 0)
  231. freezer->state = CGROUP_FREEZING;
  232. else
  233. freezer->state = CGROUP_THAWED;
  234. cgroup_iter_end(cgroup, &it);
  235. }
  236. static int freezer_read(struct cgroup *cgroup, struct cftype *cft,
  237. struct seq_file *m)
  238. {
  239. struct freezer *freezer;
  240. enum freezer_state state;
  241. if (!cgroup_lock_live_group(cgroup))
  242. return -ENODEV;
  243. freezer = cgroup_freezer(cgroup);
  244. spin_lock_irq(&freezer->lock);
  245. state = freezer->state;
  246. if (state == CGROUP_FREEZING) {
  247. /* We change from FREEZING to FROZEN lazily if the cgroup was
  248. * only partially frozen when we exitted write. */
  249. update_freezer_state(cgroup, freezer);
  250. state = freezer->state;
  251. }
  252. spin_unlock_irq(&freezer->lock);
  253. cgroup_unlock();
  254. seq_puts(m, freezer_state_strs[state]);
  255. seq_putc(m, '\n');
  256. return 0;
  257. }
  258. static int try_to_freeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
  259. {
  260. struct cgroup_iter it;
  261. struct task_struct *task;
  262. unsigned int num_cant_freeze_now = 0;
  263. freezer->state = CGROUP_FREEZING;
  264. cgroup_iter_start(cgroup, &it);
  265. while ((task = cgroup_iter_next(cgroup, &it))) {
  266. if (!freeze_task(task, true))
  267. continue;
  268. if (is_task_frozen_enough(task))
  269. continue;
  270. if (!freezing(task) && !freezer_should_skip(task))
  271. num_cant_freeze_now++;
  272. }
  273. cgroup_iter_end(cgroup, &it);
  274. return num_cant_freeze_now ? -EBUSY : 0;
  275. }
  276. static void unfreeze_cgroup(struct cgroup *cgroup, struct freezer *freezer)
  277. {
  278. struct cgroup_iter it;
  279. struct task_struct *task;
  280. cgroup_iter_start(cgroup, &it);
  281. while ((task = cgroup_iter_next(cgroup, &it))) {
  282. thaw_process(task);
  283. }
  284. cgroup_iter_end(cgroup, &it);
  285. freezer->state = CGROUP_THAWED;
  286. }
  287. static int freezer_change_state(struct cgroup *cgroup,
  288. enum freezer_state goal_state)
  289. {
  290. struct freezer *freezer;
  291. int retval = 0;
  292. freezer = cgroup_freezer(cgroup);
  293. spin_lock_irq(&freezer->lock);
  294. update_freezer_state(cgroup, freezer);
  295. if (goal_state == freezer->state)
  296. goto out;
  297. switch (goal_state) {
  298. case CGROUP_THAWED:
  299. unfreeze_cgroup(cgroup, freezer);
  300. break;
  301. case CGROUP_FROZEN:
  302. retval = try_to_freeze_cgroup(cgroup, freezer);
  303. break;
  304. default:
  305. BUG();
  306. }
  307. out:
  308. spin_unlock_irq(&freezer->lock);
  309. return retval;
  310. }
  311. static int freezer_write(struct cgroup *cgroup,
  312. struct cftype *cft,
  313. const char *buffer)
  314. {
  315. int retval;
  316. enum freezer_state goal_state;
  317. if (strcmp(buffer, freezer_state_strs[CGROUP_THAWED]) == 0)
  318. goal_state = CGROUP_THAWED;
  319. else if (strcmp(buffer, freezer_state_strs[CGROUP_FROZEN]) == 0)
  320. goal_state = CGROUP_FROZEN;
  321. else
  322. return -EINVAL;
  323. if (!cgroup_lock_live_group(cgroup))
  324. return -ENODEV;
  325. retval = freezer_change_state(cgroup, goal_state);
  326. cgroup_unlock();
  327. return retval;
  328. }
  329. static struct cftype files[] = {
  330. {
  331. .name = "state",
  332. .read_seq_string = freezer_read,
  333. .write_string = freezer_write,
  334. },
  335. };
  336. static int freezer_populate(struct cgroup_subsys *ss, struct cgroup *cgroup)
  337. {
  338. if (!cgroup->parent)
  339. return 0;
  340. return cgroup_add_files(cgroup, ss, files, ARRAY_SIZE(files));
  341. }
  342. struct cgroup_subsys freezer_subsys = {
  343. .name = "freezer",
  344. .create = freezer_create,
  345. .destroy = freezer_destroy,
  346. .populate = freezer_populate,
  347. .subsys_id = freezer_subsys_id,
  348. .can_attach = freezer_can_attach,
  349. .attach = NULL,
  350. .fork = freezer_fork,
  351. .exit = NULL,
  352. };