legacy_freezer.c 12 KB

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