cgroup_freezer.c 12 KB

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