cgroup_freezer.c 14 KB

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