legacy_freezer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * cgroup_freezer.c - control group freezer subsystem
  3. *
  4. * Copyright IBM Corporation, 2007
  5. *
  6. * Author : Cedric Le Goater <[email protected]>
  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. #include <linux/cpu.h>
  25. /*
  26. * A cgroup is freezing if any FREEZING flags are set. FREEZING_SELF is
  27. * set if "FROZEN" is written to freezer.state cgroupfs file, and cleared
  28. * for "THAWED". FREEZING_PARENT is set if the parent freezer is FREEZING
  29. * for whatever reason. IOW, a cgroup has FREEZING_PARENT set if one of
  30. * its ancestors has FREEZING_SELF set.
  31. */
  32. enum freezer_state_flags {
  33. CGROUP_FREEZER_ONLINE = (1 << 0), /* freezer is fully online */
  34. CGROUP_FREEZING_SELF = (1 << 1), /* this freezer is freezing */
  35. CGROUP_FREEZING_PARENT = (1 << 2), /* the parent freezer is freezing */
  36. CGROUP_FROZEN = (1 << 3), /* this and its descendants frozen */
  37. /* mask for all FREEZING flags */
  38. CGROUP_FREEZING = CGROUP_FREEZING_SELF | CGROUP_FREEZING_PARENT,
  39. };
  40. struct freezer {
  41. struct cgroup_subsys_state css;
  42. unsigned int state;
  43. };
  44. static DEFINE_MUTEX(freezer_mutex);
  45. static inline struct freezer *css_freezer(struct cgroup_subsys_state *css)
  46. {
  47. return css ? container_of(css, struct freezer, css) : NULL;
  48. }
  49. static inline struct freezer *task_freezer(struct task_struct *task)
  50. {
  51. return css_freezer(task_css(task, freezer_cgrp_id));
  52. }
  53. static struct freezer *parent_freezer(struct freezer *freezer)
  54. {
  55. return css_freezer(freezer->css.parent);
  56. }
  57. bool cgroup_freezing(struct task_struct *task)
  58. {
  59. bool ret;
  60. unsigned int state;
  61. rcu_read_lock();
  62. /* Check if the cgroup is still FREEZING, but not FROZEN. The extra
  63. * !FROZEN check is required, because the FREEZING bit is not cleared
  64. * when the state FROZEN is reached.
  65. */
  66. state = task_freezer(task)->state;
  67. ret = (state & CGROUP_FREEZING) && !(state & CGROUP_FROZEN);
  68. rcu_read_unlock();
  69. return ret;
  70. }
  71. static const char *freezer_state_strs(unsigned int state)
  72. {
  73. if (state & CGROUP_FROZEN)
  74. return "FROZEN";
  75. if (state & CGROUP_FREEZING)
  76. return "FREEZING";
  77. return "THAWED";
  78. };
  79. static struct cgroup_subsys_state *
  80. freezer_css_alloc(struct cgroup_subsys_state *parent_css)
  81. {
  82. struct freezer *freezer;
  83. freezer = kzalloc(sizeof(struct freezer), GFP_KERNEL);
  84. if (!freezer)
  85. return ERR_PTR(-ENOMEM);
  86. return &freezer->css;
  87. }
  88. /**
  89. * freezer_css_online - commit creation of a freezer css
  90. * @css: css being created
  91. *
  92. * We're committing to creation of @css. Mark it online and inherit
  93. * parent's freezing state while holding both parent's and our
  94. * freezer->lock.
  95. */
  96. static int freezer_css_online(struct cgroup_subsys_state *css)
  97. {
  98. struct freezer *freezer = css_freezer(css);
  99. struct freezer *parent = parent_freezer(freezer);
  100. cpus_read_lock();
  101. mutex_lock(&freezer_mutex);
  102. freezer->state |= CGROUP_FREEZER_ONLINE;
  103. if (parent && (parent->state & CGROUP_FREEZING)) {
  104. freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
  105. static_branch_inc_cpuslocked(&freezer_active);
  106. }
  107. mutex_unlock(&freezer_mutex);
  108. cpus_read_unlock();
  109. return 0;
  110. }
  111. /**
  112. * freezer_css_offline - initiate destruction of a freezer css
  113. * @css: css being destroyed
  114. *
  115. * @css is going away. Mark it dead and decrement system_freezing_count if
  116. * it was holding one.
  117. */
  118. static void freezer_css_offline(struct cgroup_subsys_state *css)
  119. {
  120. struct freezer *freezer = css_freezer(css);
  121. cpus_read_lock();
  122. mutex_lock(&freezer_mutex);
  123. if (freezer->state & CGROUP_FREEZING)
  124. static_branch_dec_cpuslocked(&freezer_active);
  125. freezer->state = 0;
  126. mutex_unlock(&freezer_mutex);
  127. cpus_read_unlock();
  128. }
  129. static void freezer_css_free(struct cgroup_subsys_state *css)
  130. {
  131. kfree(css_freezer(css));
  132. }
  133. /*
  134. * Tasks can be migrated into a different freezer anytime regardless of its
  135. * current state. freezer_attach() is responsible for making new tasks
  136. * conform to the current state.
  137. *
  138. * Freezer state changes and task migration are synchronized via
  139. * @freezer->lock. freezer_attach() makes the new tasks conform to the
  140. * current state and all following state changes can see the new tasks.
  141. */
  142. static void freezer_attach(struct cgroup_taskset *tset)
  143. {
  144. struct task_struct *task;
  145. struct cgroup_subsys_state *new_css;
  146. mutex_lock(&freezer_mutex);
  147. /*
  148. * Make the new tasks conform to the current state of @new_css.
  149. * For simplicity, when migrating any task to a FROZEN cgroup, we
  150. * revert it to FREEZING and let update_if_frozen() determine the
  151. * correct state later.
  152. *
  153. * Tasks in @tset are on @new_css but may not conform to its
  154. * current state before executing the following - !frozen tasks may
  155. * be visible in a FROZEN cgroup and frozen tasks in a THAWED one.
  156. */
  157. cgroup_taskset_for_each(task, new_css, tset) {
  158. struct freezer *freezer = css_freezer(new_css);
  159. if (!(freezer->state & CGROUP_FREEZING)) {
  160. __thaw_task(task);
  161. } else {
  162. freeze_task(task);
  163. /* clear FROZEN and propagate upwards */
  164. while (freezer && (freezer->state & CGROUP_FROZEN)) {
  165. freezer->state &= ~CGROUP_FROZEN;
  166. freezer = parent_freezer(freezer);
  167. }
  168. }
  169. }
  170. mutex_unlock(&freezer_mutex);
  171. }
  172. /**
  173. * freezer_fork - cgroup post fork callback
  174. * @task: a task which has just been forked
  175. *
  176. * @task has just been created and should conform to the current state of
  177. * the cgroup_freezer it belongs to. This function may race against
  178. * freezer_attach(). Losing to freezer_attach() means that we don't have
  179. * to do anything as freezer_attach() will put @task into the appropriate
  180. * state.
  181. */
  182. static void freezer_fork(struct task_struct *task)
  183. {
  184. struct freezer *freezer;
  185. /*
  186. * The root cgroup is non-freezable, so we can skip locking the
  187. * freezer. This is safe regardless of race with task migration.
  188. * If we didn't race or won, skipping is obviously the right thing
  189. * to do. If we lost and root is the new cgroup, noop is still the
  190. * right thing to do.
  191. */
  192. if (task_css_is_root(task, freezer_cgrp_id))
  193. return;
  194. mutex_lock(&freezer_mutex);
  195. rcu_read_lock();
  196. freezer = task_freezer(task);
  197. if (freezer->state & CGROUP_FREEZING)
  198. freeze_task(task);
  199. rcu_read_unlock();
  200. mutex_unlock(&freezer_mutex);
  201. }
  202. /**
  203. * update_if_frozen - update whether a cgroup finished freezing
  204. * @css: css of interest
  205. *
  206. * Once FREEZING is initiated, transition to FROZEN is lazily updated by
  207. * calling this function. If the current state is FREEZING but not FROZEN,
  208. * this function checks whether all tasks of this cgroup and the descendant
  209. * cgroups finished freezing and, if so, sets FROZEN.
  210. *
  211. * The caller is responsible for grabbing RCU read lock and calling
  212. * update_if_frozen() on all descendants prior to invoking this function.
  213. *
  214. * Task states and freezer state might disagree while tasks are being
  215. * migrated into or out of @css, so we can't verify task states against
  216. * @freezer state here. See freezer_attach() for details.
  217. */
  218. static void update_if_frozen(struct cgroup_subsys_state *css)
  219. {
  220. struct freezer *freezer = css_freezer(css);
  221. struct cgroup_subsys_state *pos;
  222. struct css_task_iter it;
  223. struct task_struct *task;
  224. lockdep_assert_held(&freezer_mutex);
  225. if (!(freezer->state & CGROUP_FREEZING) ||
  226. (freezer->state & CGROUP_FROZEN))
  227. return;
  228. /* are all (live) children frozen? */
  229. rcu_read_lock();
  230. css_for_each_child(pos, css) {
  231. struct freezer *child = css_freezer(pos);
  232. if ((child->state & CGROUP_FREEZER_ONLINE) &&
  233. !(child->state & CGROUP_FROZEN)) {
  234. rcu_read_unlock();
  235. return;
  236. }
  237. }
  238. rcu_read_unlock();
  239. /* are all tasks frozen? */
  240. css_task_iter_start(css, 0, &it);
  241. while ((task = css_task_iter_next(&it))) {
  242. if (freezing(task) && !frozen(task))
  243. goto out_iter_end;
  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, 0, &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, 0, &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. static_branch_inc_cpuslocked(&freezer_active);
  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. freezer->state &= ~CGROUP_FROZEN;
  313. if (was_freezing)
  314. static_branch_dec_cpuslocked(&freezer_active);
  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. cpus_read_lock();
  331. /*
  332. * Update all its descendants in pre-order traversal. Each
  333. * descendant will try to inherit its parent's FREEZING state as
  334. * CGROUP_FREEZING_PARENT.
  335. */
  336. mutex_lock(&freezer_mutex);
  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. if (!css_tryget_online(pos))
  342. continue;
  343. rcu_read_unlock();
  344. if (pos_f == freezer)
  345. freezer_apply_state(pos_f, freeze,
  346. CGROUP_FREEZING_SELF);
  347. else
  348. freezer_apply_state(pos_f,
  349. parent->state & CGROUP_FREEZING,
  350. CGROUP_FREEZING_PARENT);
  351. rcu_read_lock();
  352. css_put(pos);
  353. }
  354. rcu_read_unlock();
  355. mutex_unlock(&freezer_mutex);
  356. cpus_read_unlock();
  357. }
  358. static ssize_t freezer_write(struct kernfs_open_file *of,
  359. char *buf, size_t nbytes, loff_t off)
  360. {
  361. bool freeze;
  362. buf = strstrip(buf);
  363. if (strcmp(buf, freezer_state_strs(0)) == 0)
  364. freeze = false;
  365. else if (strcmp(buf, freezer_state_strs(CGROUP_FROZEN)) == 0)
  366. freeze = true;
  367. else
  368. return -EINVAL;
  369. freezer_change_state(css_freezer(of_css(of)), freeze);
  370. return nbytes;
  371. }
  372. static u64 freezer_self_freezing_read(struct cgroup_subsys_state *css,
  373. struct cftype *cft)
  374. {
  375. struct freezer *freezer = css_freezer(css);
  376. return (bool)(freezer->state & CGROUP_FREEZING_SELF);
  377. }
  378. static u64 freezer_parent_freezing_read(struct cgroup_subsys_state *css,
  379. struct cftype *cft)
  380. {
  381. struct freezer *freezer = css_freezer(css);
  382. return (bool)(freezer->state & CGROUP_FREEZING_PARENT);
  383. }
  384. static struct cftype files[] = {
  385. {
  386. .name = "state",
  387. .flags = CFTYPE_NOT_ON_ROOT,
  388. .seq_show = freezer_read,
  389. .write = freezer_write,
  390. },
  391. {
  392. .name = "self_freezing",
  393. .flags = CFTYPE_NOT_ON_ROOT,
  394. .read_u64 = freezer_self_freezing_read,
  395. },
  396. {
  397. .name = "parent_freezing",
  398. .flags = CFTYPE_NOT_ON_ROOT,
  399. .read_u64 = freezer_parent_freezing_read,
  400. },
  401. { } /* terminate */
  402. };
  403. struct cgroup_subsys freezer_cgrp_subsys = {
  404. .css_alloc = freezer_css_alloc,
  405. .css_online = freezer_css_online,
  406. .css_offline = freezer_css_offline,
  407. .css_free = freezer_css_free,
  408. .attach = freezer_attach,
  409. .fork = freezer_fork,
  410. .legacy_cftypes = files,
  411. };
  412. EXPORT_SYMBOL_GPL(freezer_cgrp_subsys);