cgroup_freezer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. /*
  24. * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is
  25. * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
  26. * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING
  27. * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of
  28. * its ancestors has FREEZING_SELF set.
  29. */
  30. enum freezer_state_flags {
  31. CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */
  32. CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */
  33. CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */
  34. CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */
  35. /* mask for all FREEZING flags */
  36. CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
  37. };
  38. struct freezer {
  39. struct cgroup_subsys_state css;
  40. unsigned int state;
  41. spinlock_t lock;
  42. };
  43. static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
  44. {
  45. return css ? container_of(css, struct freezer, css) : NULL;
  46. }
  47. static inline struct freezer *task_freezer(struct task_struct *task)
  48. {
  49. return css_freezer(task_css(task, freezer_subsys_id));
  50. }
  51. static struct freezer *parent_freezer(struct freezer *freezer)
  52. {
  53. return css_freezer(css_parent(&freezer->css));
  54. }
  55. bool cgroup_freezing(struct task_struct *task)
  56. {
  57. bool ret;
  58. rcu_read_lock();
  59. ret = task_freezer(task)->state & CGROUP_FREEZING;
  60. rcu_read_unlock();
  61. return ret;
  62. }
  63. /*
  64. * cgroups_write_string() limits the size of freezer state strings to
  65. * CGROUP_LOCAL_BUFFER_SIZE
  66. */
  67. static const char *freezer_state_strs(unsigned int state)
  68. {
  69. if (state & CGROUP_FROZEN)
  70. return "FROZEN";
  71. if (state & CGROUP_FREEZING)
  72. return "FREEZING";
  73. return "THAWED";
  74. };
  75. struct cgroup_subsys freezer_subsys;
  76. static struct cgroup_subsys_state *
  77. freezer_css_alloc(struct cgroup_subsys_state *parent_css)
  78. {
  79. struct freezer *freezer;
  80. freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
  81. if (!freezer)
  82. return ERR_PTR(-ENOMEM);
  83. spin_lock_init(&freezer->lock);
  84. return &freezer->css;
  85. }
  86. /**
  87. * freezer_css_online - commit creation of a freezer css
  88. * @css: css being created
  89. *
  90. * We're committing to creation of @css. Mark it online and inherit
  91. * parent's freezing state while holding both parent's and our
  92. * freezer->lock.
  93. */
  94. static int freezer_css_online(struct cgroup_subsys_state *css)
  95. {
  96. struct freezer *freezer = css_freezer(css);
  97. struct freezer *parent = parent_freezer(freezer);
  98. /*
  99. * The following double locking and freezing state inheritance
  100. * guarantee that @cgroup can never escape ancestors' freezing
  101. * states. See css_for_each_descendant_pre() for details.
  102. */
  103. if (parent)
  104. spin_lock_irq(&parent->lock);
  105. spin_lock_nested(&freezer->lock, SINGLE_DEPTH_NESTING);
  106. freezer->state |= CGROUP_FREEZER_ONLINE;
  107. if (parent && (parent->state & CGROUP_FREEZING)) {
  108. freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
  109. atomic_inc(&system_freezing_cnt);
  110. }
  111. spin_unlock(&freezer->lock);
  112. if (parent)
  113. spin_unlock_irq(&parent->lock);
  114. return 0;
  115. }
  116. /**
  117. * freezer_css_offline - initiate destruction of a freezer css
  118. * @css: css being destroyed
  119. *
  120. * @css is going away. Mark it dead and decrement system_freezing_count if
  121. * it was holding one.
  122. */
  123. static void freezer_css_offline(struct cgroup_subsys_state *css)
  124. {
  125. struct freezer *freezer = css_freezer(css);
  126. spin_lock_irq(&freezer->lock);
  127. if (freezer->state & CGROUP_FREEZING)
  128. atomic_dec(&system_freezing_cnt);
  129. freezer->state = 0;
  130. spin_unlock_irq(&freezer->lock);
  131. }
  132. static void freezer_css_free(struct cgroup_subsys_state *css)
  133. {
  134. kfree(css_freezer(css));
  135. }
  136. /*
  137. * Tasks can be migrated into a different freezer anytime regardless of its
  138. * current state. freezer_attach() is responsible for making new tasks
  139. * conform to the current state.
  140. *
  141. * Freezer state changes and task migration are synchronized via
  142. * @freezer->lock. freezer_attach() makes the new tasks conform to the
  143. * current state and all following state changes can see the new tasks.
  144. */
  145. static void freezer_attach(struct cgroup_subsys_state *new_css,
  146. struct cgroup_taskset *tset)
  147. {
  148. struct freezer *freezer = css_freezer(new_css);
  149. struct task_struct *task;
  150. bool clear_frozen = false;
  151. spin_lock_irq(&freezer->lock);
  152. /*
  153. * Make the new tasks conform to the current state of @new_css.
  154. * For simplicity, when migrating any task to a FROZEN cgroup, we
  155. * revert it to FREEZING and let update_if_frozen() determine the
  156. * correct state later.
  157. *
  158. * Tasks in @tset are on @new_css but may not conform to its
  159. * current state before executing the following - !frozen tasks may
  160. * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
  161. */
  162. cgroup_taskset_for_each(task, new_css, tset) {
  163. if (!(freezer->state & CGROUP_FREEZING)) {
  164. __thaw_task(task);
  165. } else {
  166. freeze_task(task);
  167. freezer->state &= ~CGROUP_FROZEN;
  168. clear_frozen = true;
  169. }
  170. }
  171. spin_unlock_irq(&freezer->lock);
  172. /*
  173. * Propagate FROZEN clearing upwards. We may race with
  174. * update_if_frozen(), but as long as both work bottom-up, either
  175. * update_if_frozen() sees child's FROZEN cleared or we clear the
  176. * parent's FROZEN later. No parent w/ !FROZEN children can be
  177. * left FROZEN.
  178. */
  179. while (clear_frozen && (freezer = parent_freezer(freezer))) {
  180. spin_lock_irq(&freezer->lock);
  181. freezer->state &= ~CGROUP_FROZEN;
  182. clear_frozen = freezer->state & CGROUP_FREEZING;
  183. spin_unlock_irq(&freezer->lock);
  184. }
  185. }
  186. static void freezer_fork(struct task_struct *task)
  187. {
  188. struct freezer *freezer;
  189. rcu_read_lock();
  190. freezer = task_freezer(task);
  191. /*
  192. * The root cgroup is non-freezable, so we can skip the
  193. * following check.
  194. */
  195. if (!parent_freezer(freezer))
  196. goto out;
  197. spin_lock_irq(&freezer->lock);
  198. if (freezer->state & CGROUP_FREEZING)
  199. freeze_task(task);
  200. spin_unlock_irq(&freezer->lock);
  201. out:
  202. rcu_read_unlock();
  203. }
  204. /**
  205. * update_if_frozen - update whether a cgroup finished freezing
  206. * @css: css of interest
  207. *
  208. * Once FREEZING is initiated, transition to FROZEN is lazily updated by
  209. * calling this function. If the current state is FREEZING but not FROZEN,
  210. * this function checks whether all tasks of this cgroup and the descendant
  211. * cgroups finished freezing and, if so, sets FROZEN.
  212. *
  213. * The caller is responsible for grabbing RCU read lock and calling
  214. * update_if_frozen() on all descendants prior to invoking this function.
  215. *
  216. * Task states and freezer state might disagree while tasks are being
  217. * migrated into or out of @css, so we can't verify task states against
  218. * @freezer state here. See freezer_attach() for details.
  219. */
  220. static void update_if_frozen(struct cgroup_subsys_state *css)
  221. {
  222. struct freezer *freezer = css_freezer(css);
  223. struct cgroup_subsys_state *pos;
  224. struct css_task_iter it;
  225. struct task_struct *task;
  226. WARN_ON_ONCE(!rcu_read_lock_held());
  227. spin_lock_irq(&freezer->lock);
  228. if (!(freezer->state & CGROUP_FREEZING) ||
  229. (freezer->state & CGROUP_FROZEN))
  230. goto out_unlock;
  231. /* are all (live) children frozen? */
  232. css_for_each_child(pos, css) {
  233. struct freezer *child = css_freezer(pos);
  234. if ((child->state & CGROUP_FREEZER_ONLINE) &&
  235. !(child->state & CGROUP_FROZEN))
  236. goto out_unlock;
  237. }
  238. /* are all tasks frozen? */
  239. css_task_iter_start(css, &it);
  240. while ((task = css_task_iter_next(&it))) {
  241. if (freezing(task)) {
  242. /*
  243. * freezer_should_skip() indicates that the task
  244. * should be skipped when determining freezing
  245. * completion. Consider it frozen in addition to
  246. * the usual frozen condition.
  247. */
  248. if (!frozen(task) && !freezer_should_skip(task))
  249. goto out_iter_end;
  250. }
  251. }
  252. freezer->state |= CGROUP_FROZEN;
  253. out_iter_end:
  254. css_task_iter_end(&it);
  255. out_unlock:
  256. spin_unlock_irq(&freezer->lock);
  257. }
  258. static int freezer_read(struct cgroup_subsys_state *css, struct cftype *cft,
  259. struct seq_file *m)
  260. {
  261. struct cgroup_subsys_state *pos;
  262. rcu_read_lock();
  263. /* update states bottom-up */
  264. css_for_each_descendant_post(pos, css)
  265. update_if_frozen(pos);
  266. rcu_read_unlock();
  267. seq_puts(m, freezer_state_strs(css_freezer(css)->state));
  268. seq_putc(m, '\n');
  269. return 0;
  270. }
  271. static void freeze_cgroup(struct freezer *freezer)
  272. {
  273. struct css_task_iter it;
  274. struct task_struct *task;
  275. css_task_iter_start(&freezer->css, &it);
  276. while ((task = css_task_iter_next(&it)))
  277. freeze_task(task);
  278. css_task_iter_end(&it);
  279. }
  280. static void unfreeze_cgroup(struct freezer *freezer)
  281. {
  282. struct css_task_iter it;
  283. struct task_struct *task;
  284. css_task_iter_start(&freezer->css, &it);
  285. while ((task = css_task_iter_next(&it)))
  286. __thaw_task(task);
  287. css_task_iter_end(&it);
  288. }
  289. /**
  290. * freezer_apply_state - apply state change to a single cgroup_freezer
  291. * @freezer: freezer to apply state change to
  292. * @freeze: whether to freeze or unfreeze
  293. * @state: CGROUP_FREEZING_* flag to set or clear
  294. *
  295. * Set or clear @state on @cgroup according to @freeze, and perform
  296. * freezing or thawing as necessary.
  297. */
  298. static void freezer_apply_state(struct freezer *freezer, bool freeze,
  299. unsigned int state)
  300. {
  301. /* also synchronizes against task migration, see freezer_attach() */
  302. lockdep_assert_held(&freezer->lock);
  303. if (!(freezer->state & CGROUP_FREEZER_ONLINE))
  304. return;
  305. if (freeze) {
  306. if (!(freezer->state & CGROUP_FREEZING))
  307. atomic_inc(&system_freezing_cnt);
  308. freezer->state |= state;
  309. freeze_cgroup(freezer);
  310. } else {
  311. bool was_freezing = freezer->state & CGROUP_FREEZING;
  312. freezer->state &= ~state;
  313. if (!(freezer->state & CGROUP_FREEZING)) {
  314. if (was_freezing)
  315. atomic_dec(&system_freezing_cnt);
  316. freezer->state &= ~CGROUP_FROZEN;
  317. unfreeze_cgroup(freezer);
  318. }
  319. }
  320. }
  321. /**
  322. * freezer_change_state - change the freezing state of a cgroup_freezer
  323. * @freezer: freezer of interest
  324. * @freeze: whether to freeze or thaw
  325. *
  326. * Freeze or thaw @freezer according to @freeze. The operations are
  327. * recursive - all descendants of @freezer will be affected.
  328. */
  329. static void freezer_change_state(struct freezer *freezer, bool freeze)
  330. {
  331. struct cgroup_subsys_state *pos;
  332. /*
  333. * Update all its descendants in pre-order traversal. Each
  334. * descendant will try to inherit its parent's FREEZING state as
  335. * CGROUP_FREEZING_PARENT.
  336. */
  337. rcu_read_lock();
  338. css_for_each_descendant_pre(pos, &freezer->css) {
  339. struct freezer *pos_f = css_freezer(pos);
  340. struct freezer *parent = parent_freezer(pos_f);
  341. spin_lock_irq(&pos_f->lock);
  342. if (pos_f == freezer) {
  343. freezer_apply_state(pos_f, freeze,
  344. CGROUP_FREEZING_SELF);
  345. } else {
  346. /*
  347. * Our update to @parent->state is already visible
  348. * which is all we need. No need to lock @parent.
  349. * For more info on synchronization, see
  350. * freezer_post_create().
  351. */
  352. freezer_apply_state(pos_f,
  353. parent->state & CGROUP_FREEZING,
  354. CGROUP_FREEZING_PARENT);
  355. }
  356. spin_unlock_irq(&pos_f->lock);
  357. }
  358. rcu_read_unlock();
  359. }
  360. static int freezer_write(struct cgroup_subsys_state *css, struct cftype *cft,
  361. const char *buffer)
  362. {
  363. bool freeze;
  364. if (strcmp(buffer, freezer_state_strs(0)) == 0)
  365. freeze = false;
  366. else if (strcmp(buffer, freezer_state_strs(CGROUP_FROZEN)) == 0)
  367. freeze = true;
  368. else
  369. return -EINVAL;
  370. freezer_change_state(css_freezer(css), freeze);
  371. return 0;
  372. }
  373. static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
  374. struct cftype *cft)
  375. {
  376. struct freezer *freezer = css_freezer(css);
  377. return (bool)(freezer->state & CGROUP_FREEZING_SELF);
  378. }
  379. static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
  380. struct cftype *cft)
  381. {
  382. struct freezer *freezer = css_freezer(css);
  383. return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
  384. }
  385. static struct cftype files[] = {
  386. {
  387. .name = "state",
  388. .flags = CFTYPE_NOT_ON_ROOT,
  389. .read_seq_string = freezer_read,
  390. .write_string = freezer_write,
  391. },
  392. {
  393. .name = "self_freezing",
  394. .flags = CFTYPE_NOT_ON_ROOT,
  395. .read_u64 = freezer_self_freezing_read,
  396. },
  397. {
  398. .name = "parent_freezing",
  399. .flags = CFTYPE_NOT_ON_ROOT,
  400. .read_u64 = freezer_parent_freezing_read,
  401. },
  402. { } /* terminate */
  403. };
  404. struct cgroup_subsys freezer_subsys = {
  405. .name = "freezer",
  406. .css_alloc = freezer_css_alloc,
  407. .css_online = freezer_css_online,
  408. .css_offline = freezer_css_offline,
  409. .css_free = freezer_css_free,
  410. .subsys_id = freezer_subsys_id,
  411. .attach = freezer_attach,
  412. .fork = freezer_fork,
  413. .base_cftypes = files,
  414. };