cgroup_freezer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #include <linux/mutex.h>
  24. /*
  25. * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is
  26. * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
  27. * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING
  28. * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of
  29. * its ancestors has FREEZING_SELF set.
  30. */
  31. enum freezer_state_flags {
  32. CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */
  33. CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */
  34. CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */
  35. CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */
  36. /* mask for all FREEZING flags */
  37. CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
  38. };
  39. struct freezer {
  40. struct cgroup_subsys_state css;
  41. unsigned int state;
  42. };
  43. static DEFINE_MUTEX(freezer_mutex);
  44. static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
  45. {
  46. return css ? container_of(css, struct freezer, css) : NULL;
  47. }
  48. static inline struct freezer *task_freezer(struct task_struct *task)
  49. {
  50. return css_freezer(task_css(task, freezer_cgrp_id));
  51. }
  52. static struct freezer *parent_freezer(struct freezer *freezer)
  53. {
  54. return css_freezer(css_parent(&freezer->css));
  55. }
  56. bool cgroup_freezing(struct task_struct *task)
  57. {
  58. bool ret;
  59. rcu_read_lock();
  60. ret = task_freezer(task)->state & CGROUP_FREEZING;
  61. rcu_read_unlock();
  62. return ret;
  63. }
  64. /*
  65. * cgroups_write_string() limits the size of freezer state strings to
  66. * CGROUP_LOCAL_BUFFER_SIZE
  67. */
  68. static const char *freezer_state_strs(unsigned int state)
  69. {
  70. if (state & CGROUP_FROZEN)
  71. return "FROZEN";
  72. if (state & CGROUP_FREEZING)
  73. return "FREEZING";
  74. return "THAWED";
  75. };
  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. 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. mutex_lock(&freezer_mutex);
  98. freezer->state |= CGROUP_FREEZER_ONLINE;
  99. if (parent && (parent->state & CGROUP_FREEZING)) {
  100. freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
  101. atomic_inc(&system_freezing_cnt);
  102. }
  103. mutex_unlock(&freezer_mutex);
  104. return 0;
  105. }
  106. /**
  107. * freezer_css_offline - initiate destruction of a freezer css
  108. * @css: css being destroyed
  109. *
  110. * @css is going away. Mark it dead and decrement system_freezing_count if
  111. * it was holding one.
  112. */
  113. static void freezer_css_offline(struct cgroup_subsys_state *css)
  114. {
  115. struct freezer *freezer = css_freezer(css);
  116. mutex_lock(&freezer_mutex);
  117. if (freezer->state & CGROUP_FREEZING)
  118. atomic_dec(&system_freezing_cnt);
  119. freezer->state = 0;
  120. mutex_unlock(&freezer_mutex);
  121. }
  122. static void freezer_css_free(struct cgroup_subsys_state *css)
  123. {
  124. kfree(css_freezer(css));
  125. }
  126. /*
  127. * Tasks can be migrated into a different freezer anytime regardless of its
  128. * current state. freezer_attach() is responsible for making new tasks
  129. * conform to the current state.
  130. *
  131. * Freezer state changes and task migration are synchronized via
  132. * @freezer->lock. freezer_attach() makes the new tasks conform to the
  133. * current state and all following state changes can see the new tasks.
  134. */
  135. static void freezer_attach(struct cgroup_subsys_state *new_css,
  136. struct cgroup_taskset *tset)
  137. {
  138. struct freezer *freezer = css_freezer(new_css);
  139. struct task_struct *task;
  140. bool clear_frozen = false;
  141. mutex_lock(&freezer_mutex);
  142. /*
  143. * Make the new tasks conform to the current state of @new_css.
  144. * For simplicity, when migrating any task to a FROZEN cgroup, we
  145. * revert it to FREEZING and let update_if_frozen() determine the
  146. * correct state later.
  147. *
  148. * Tasks in @tset are on @new_css but may not conform to its
  149. * current state before executing the following - !frozen tasks may
  150. * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
  151. */
  152. cgroup_taskset_for_each(task, tset) {
  153. if (!(freezer->state & CGROUP_FREEZING)) {
  154. __thaw_task(task);
  155. } else {
  156. freeze_task(task);
  157. freezer->state &= ~CGROUP_FROZEN;
  158. clear_frozen = true;
  159. }
  160. }
  161. /* propagate FROZEN clearing upwards */
  162. while (clear_frozen && (freezer = parent_freezer(freezer))) {
  163. freezer->state &= ~CGROUP_FROZEN;
  164. clear_frozen = freezer->state & CGROUP_FREEZING;
  165. }
  166. mutex_unlock(&freezer_mutex);
  167. }
  168. /**
  169. * freezer_fork - cgroup post fork callback
  170. * @task: a task which has just been forked
  171. *
  172. * @task has just been created and should conform to the current state of
  173. * the cgroup_freezer it belongs to. This function may race against
  174. * freezer_attach(). Losing to freezer_attach() means that we don't have
  175. * to do anything as freezer_attach() will put @task into the appropriate
  176. * state.
  177. */
  178. static void freezer_fork(struct task_struct *task)
  179. {
  180. struct freezer *freezer;
  181. /*
  182. * The root cgroup is non-freezable, so we can skip locking the
  183. * freezer. This is safe regardless of race with task migration.
  184. * If we didn't race or won, skipping is obviously the right thing
  185. * to do. If we lost and root is the new cgroup, noop is still the
  186. * right thing to do.
  187. */
  188. if (task_css_is_root(task, freezer_cgrp_id))
  189. return;
  190. mutex_lock(&freezer_mutex);
  191. rcu_read_lock();
  192. freezer = task_freezer(task);
  193. if (freezer->state & CGROUP_FREEZING)
  194. freeze_task(task);
  195. rcu_read_unlock();
  196. mutex_unlock(&freezer_mutex);
  197. }
  198. /**
  199. * update_if_frozen - update whether a cgroup finished freezing
  200. * @css: css of interest
  201. *
  202. * Once FREEZING is initiated, transition to FROZEN is lazily updated by
  203. * calling this function. If the current state is FREEZING but not FROZEN,
  204. * this function checks whether all tasks of this cgroup and the descendant
  205. * cgroups finished freezing and, if so, sets FROZEN.
  206. *
  207. * The caller is responsible for grabbing RCU read lock and calling
  208. * update_if_frozen() on all descendants prior to invoking this function.
  209. *
  210. * Task states and freezer state might disagree while tasks are being
  211. * migrated into or out of @css, so we can't verify task states against
  212. * @freezer state here. See freezer_attach() for details.
  213. */
  214. static void update_if_frozen(struct cgroup_subsys_state *css)
  215. {
  216. struct freezer *freezer = css_freezer(css);
  217. struct cgroup_subsys_state *pos;
  218. struct css_task_iter it;
  219. struct task_struct *task;
  220. lockdep_assert_held(&freezer_mutex);
  221. if (!(freezer->state & CGROUP_FREEZING) ||
  222. (freezer->state & CGROUP_FROZEN))
  223. return;
  224. /* are all (live) children frozen? */
  225. rcu_read_lock();
  226. css_for_each_child(pos, css) {
  227. struct freezer *child = css_freezer(pos);
  228. if ((child->state & CGROUP_FREEZER_ONLINE) &&
  229. !(child->state & CGROUP_FROZEN)) {
  230. rcu_read_unlock();
  231. return;
  232. }
  233. }
  234. rcu_read_unlock();
  235. /* are all tasks frozen? */
  236. css_task_iter_start(css, &it);
  237. while ((task = css_task_iter_next(&it))) {
  238. if (freezing(task)) {
  239. /*
  240. * freezer_should_skip() indicates that the task
  241. * should be skipped when determining freezing
  242. * completion. Consider it frozen in addition to
  243. * the usual frozen condition.
  244. */
  245. if (!frozen(task) && !freezer_should_skip(task))
  246. goto out_iter_end;
  247. }
  248. }
  249. freezer->state |= CGROUP_FROZEN;
  250. out_iter_end:
  251. css_task_iter_end(&it);
  252. }
  253. static int freezer_read(struct seq_file *m, void *v)
  254. {
  255. struct cgroup_subsys_state *css = seq_css(m), *pos;
  256. mutex_lock(&freezer_mutex);
  257. rcu_read_lock();
  258. /* update states bottom-up */
  259. css_for_each_descendant_post(pos, css) {
  260. if (!css_tryget(pos))
  261. continue;
  262. rcu_read_unlock();
  263. update_if_frozen(pos);
  264. rcu_read_lock();
  265. css_put(pos);
  266. }
  267. rcu_read_unlock();
  268. mutex_unlock(&freezer_mutex);
  269. seq_puts(m, freezer_state_strs(css_freezer(css)->state));
  270. seq_putc(m, '\n');
  271. return 0;
  272. }
  273. static void freeze_cgroup(struct freezer *freezer)
  274. {
  275. struct css_task_iter it;
  276. struct task_struct *task;
  277. css_task_iter_start(&freezer->css, &it);
  278. while ((task = css_task_iter_next(&it)))
  279. freeze_task(task);
  280. css_task_iter_end(&it);
  281. }
  282. static void unfreeze_cgroup(struct freezer *freezer)
  283. {
  284. struct css_task_iter it;
  285. struct task_struct *task;
  286. css_task_iter_start(&freezer->css, &it);
  287. while ((task = css_task_iter_next(&it)))
  288. __thaw_task(task);
  289. css_task_iter_end(&it);
  290. }
  291. /**
  292. * freezer_apply_state - apply state change to a single cgroup_freezer
  293. * @freezer: freezer to apply state change to
  294. * @freeze: whether to freeze or unfreeze
  295. * @state: CGROUP_FREEZING_* flag to set or clear
  296. *
  297. * Set or clear @state on @cgroup according to @freeze, and perform
  298. * freezing or thawing as necessary.
  299. */
  300. static void freezer_apply_state(struct freezer *freezer, bool freeze,
  301. unsigned int state)
  302. {
  303. /* also synchronizes against task migration, see freezer_attach() */
  304. lockdep_assert_held(&freezer_mutex);
  305. if (!(freezer->state & CGROUP_FREEZER_ONLINE))
  306. return;
  307. if (freeze) {
  308. if (!(freezer->state & CGROUP_FREEZING))
  309. atomic_inc(&system_freezing_cnt);
  310. freezer->state |= state;
  311. freeze_cgroup(freezer);
  312. } else {
  313. bool was_freezing = freezer->state & CGROUP_FREEZING;
  314. freezer->state &= ~state;
  315. if (!(freezer->state & CGROUP_FREEZING)) {
  316. if (was_freezing)
  317. atomic_dec(&system_freezing_cnt);
  318. freezer->state &= ~CGROUP_FROZEN;
  319. unfreeze_cgroup(freezer);
  320. }
  321. }
  322. }
  323. /**
  324. * freezer_change_state - change the freezing state of a cgroup_freezer
  325. * @freezer: freezer of interest
  326. * @freeze: whether to freeze or thaw
  327. *
  328. * Freeze or thaw @freezer according to @freeze. The operations are
  329. * recursive - all descendants of @freezer will be affected.
  330. */
  331. static void freezer_change_state(struct freezer *freezer, bool freeze)
  332. {
  333. struct cgroup_subsys_state *pos;
  334. /*
  335. * Update all its descendants in pre-order traversal. Each
  336. * descendant will try to inherit its parent's FREEZING state as
  337. * CGROUP_FREEZING_PARENT.
  338. */
  339. mutex_lock(&freezer_mutex);
  340. rcu_read_lock();
  341. css_for_each_descendant_pre(pos, &freezer->css) {
  342. struct freezer *pos_f = css_freezer(pos);
  343. struct freezer *parent = parent_freezer(pos_f);
  344. if (!css_tryget(pos))
  345. continue;
  346. rcu_read_unlock();
  347. if (pos_f == freezer)
  348. freezer_apply_state(pos_f, freeze,
  349. CGROUP_FREEZING_SELF);
  350. else
  351. freezer_apply_state(pos_f,
  352. parent->state & CGROUP_FREEZING,
  353. CGROUP_FREEZING_PARENT);
  354. rcu_read_lock();
  355. css_put(pos);
  356. }
  357. rcu_read_unlock();
  358. mutex_unlock(&freezer_mutex);
  359. }
  360. static int freezer_write(struct cgroup_subsys_state *css, struct cftype *cft,
  361. 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. .seq_show = 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_cgrp_subsys = {
  405. .css_alloc = freezer_css_alloc,
  406. .css_online = freezer_css_online,
  407. .css_offline = freezer_css_offline,
  408. .css_free = freezer_css_free,
  409. .attach = freezer_attach,
  410. .fork = freezer_fork,
  411. .base_cftypes = files,
  412. };