cgroup_freezer.c 9.4 KB

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