legacy_freezer.c 12 KB

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