freezer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/cgroup.h>
  3. #include <linux/sched.h>
  4. #include <linux/sched/task.h>
  5. #include <linux/sched/signal.h>
  6. #include "cgroup-internal.h"
  7. #include <trace/events/cgroup.h>
  8. /*
  9. * Update CGRP_FROZEN of cgroup.flag
  10. * Return true if flags is updated; false if flags has no change
  11. */
  12. static bool cgroup_update_frozen_flag(struct cgroup *cgrp, bool frozen)
  13. {
  14. lockdep_assert_held(&css_set_lock);
  15. /* Already there? */
  16. if (test_bit(CGRP_FROZEN, &cgrp->flags) == frozen)
  17. return false;
  18. if (frozen)
  19. set_bit(CGRP_FROZEN, &cgrp->flags);
  20. else
  21. clear_bit(CGRP_FROZEN, &cgrp->flags);
  22. cgroup_file_notify(&cgrp->events_file);
  23. TRACE_CGROUP_PATH(notify_frozen, cgrp, frozen);
  24. return true;
  25. }
  26. /*
  27. * Propagate the cgroup frozen state upwards by the cgroup tree.
  28. */
  29. static void cgroup_propagate_frozen(struct cgroup *cgrp, bool frozen)
  30. {
  31. int desc = 1;
  32. /*
  33. * If the new state is frozen, some freezing ancestor cgroups may change
  34. * their state too, depending on if all their descendants are frozen.
  35. *
  36. * Otherwise, all ancestor cgroups are forced into the non-frozen state.
  37. */
  38. while ((cgrp = cgroup_parent(cgrp))) {
  39. if (frozen) {
  40. cgrp->freezer.nr_frozen_descendants += desc;
  41. if (!test_bit(CGRP_FREEZE, &cgrp->flags) ||
  42. (cgrp->freezer.nr_frozen_descendants !=
  43. cgrp->nr_descendants))
  44. continue;
  45. } else {
  46. cgrp->freezer.nr_frozen_descendants -= desc;
  47. }
  48. if (cgroup_update_frozen_flag(cgrp, frozen))
  49. desc++;
  50. }
  51. }
  52. /*
  53. * Revisit the cgroup frozen state.
  54. * Checks if the cgroup is really frozen and perform all state transitions.
  55. */
  56. void cgroup_update_frozen(struct cgroup *cgrp)
  57. {
  58. bool frozen;
  59. /*
  60. * If the cgroup has to be frozen (CGRP_FREEZE bit set),
  61. * and all tasks are frozen and/or stopped, let's consider
  62. * the cgroup frozen. Otherwise it's not frozen.
  63. */
  64. frozen = test_bit(CGRP_FREEZE, &cgrp->flags) &&
  65. cgrp->freezer.nr_frozen_tasks == __cgroup_task_count(cgrp);
  66. /* If flags is updated, update the state of ancestor cgroups. */
  67. if (cgroup_update_frozen_flag(cgrp, frozen))
  68. cgroup_propagate_frozen(cgrp, frozen);
  69. }
  70. /*
  71. * Increment cgroup's nr_frozen_tasks.
  72. */
  73. static void cgroup_inc_frozen_cnt(struct cgroup *cgrp)
  74. {
  75. cgrp->freezer.nr_frozen_tasks++;
  76. }
  77. /*
  78. * Decrement cgroup's nr_frozen_tasks.
  79. */
  80. static void cgroup_dec_frozen_cnt(struct cgroup *cgrp)
  81. {
  82. cgrp->freezer.nr_frozen_tasks--;
  83. WARN_ON_ONCE(cgrp->freezer.nr_frozen_tasks < 0);
  84. }
  85. /*
  86. * Enter frozen/stopped state, if not yet there. Update cgroup's counters,
  87. * and revisit the state of the cgroup, if necessary.
  88. */
  89. void cgroup_enter_frozen(void)
  90. {
  91. struct cgroup *cgrp;
  92. if (current->frozen)
  93. return;
  94. spin_lock_irq(&css_set_lock);
  95. current->frozen = true;
  96. cgrp = task_dfl_cgroup(current);
  97. cgroup_inc_frozen_cnt(cgrp);
  98. cgroup_update_frozen(cgrp);
  99. spin_unlock_irq(&css_set_lock);
  100. }
  101. /*
  102. * Conditionally leave frozen/stopped state. Update cgroup's counters,
  103. * and revisit the state of the cgroup, if necessary.
  104. *
  105. * If always_leave is not set, and the cgroup is freezing,
  106. * we're racing with the cgroup freezing. In this case, we don't
  107. * drop the frozen counter to avoid a transient switch to
  108. * the unfrozen state.
  109. */
  110. void cgroup_leave_frozen(bool always_leave)
  111. {
  112. struct cgroup *cgrp;
  113. spin_lock_irq(&css_set_lock);
  114. cgrp = task_dfl_cgroup(current);
  115. if (always_leave || !test_bit(CGRP_FREEZE, &cgrp->flags)) {
  116. cgroup_dec_frozen_cnt(cgrp);
  117. cgroup_update_frozen(cgrp);
  118. WARN_ON_ONCE(!current->frozen);
  119. current->frozen = false;
  120. } else if (!(current->jobctl & JOBCTL_TRAP_FREEZE)) {
  121. spin_lock(&current->sighand->siglock);
  122. current->jobctl |= JOBCTL_TRAP_FREEZE;
  123. set_thread_flag(TIF_SIGPENDING);
  124. spin_unlock(&current->sighand->siglock);
  125. }
  126. spin_unlock_irq(&css_set_lock);
  127. }
  128. /*
  129. * Freeze or unfreeze the task by setting or clearing the JOBCTL_TRAP_FREEZE
  130. * jobctl bit.
  131. */
  132. static void cgroup_freeze_task(struct task_struct *task, bool freeze)
  133. {
  134. unsigned long flags;
  135. /* If the task is about to die, don't bother with freezing it. */
  136. if (!lock_task_sighand(task, &flags))
  137. return;
  138. if (freeze) {
  139. task->jobctl |= JOBCTL_TRAP_FREEZE;
  140. signal_wake_up(task, false);
  141. } else {
  142. task->jobctl &= ~JOBCTL_TRAP_FREEZE;
  143. wake_up_process(task);
  144. }
  145. unlock_task_sighand(task, &flags);
  146. }
  147. /*
  148. * Freeze or unfreeze all tasks in the given cgroup.
  149. */
  150. static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze, u64 ts_nsec)
  151. {
  152. struct css_task_iter it;
  153. struct task_struct *task;
  154. lockdep_assert_held(&cgroup_mutex);
  155. spin_lock_irq(&css_set_lock);
  156. write_seqcount_begin(&cgrp->freezer.freeze_seq);
  157. if (freeze) {
  158. set_bit(CGRP_FREEZE, &cgrp->flags);
  159. cgrp->freezer.freeze_start_nsec = ts_nsec;
  160. } else {
  161. clear_bit(CGRP_FREEZE, &cgrp->flags);
  162. cgrp->freezer.frozen_nsec += (ts_nsec -
  163. cgrp->freezer.freeze_start_nsec);
  164. }
  165. write_seqcount_end(&cgrp->freezer.freeze_seq);
  166. spin_unlock_irq(&css_set_lock);
  167. if (freeze)
  168. TRACE_CGROUP_PATH(freeze, cgrp);
  169. else
  170. TRACE_CGROUP_PATH(unfreeze, cgrp);
  171. css_task_iter_start(&cgrp->self, 0, &it);
  172. while ((task = css_task_iter_next(&it))) {
  173. /*
  174. * Ignore kernel threads here. Freezing cgroups containing
  175. * kthreads isn't supported.
  176. */
  177. if (task->flags & PF_KTHREAD)
  178. continue;
  179. cgroup_freeze_task(task, freeze);
  180. }
  181. css_task_iter_end(&it);
  182. /*
  183. * Cgroup state should be revisited here to cover empty leaf cgroups
  184. * and cgroups which descendants are already in the desired state.
  185. */
  186. spin_lock_irq(&css_set_lock);
  187. if (cgrp->nr_descendants == cgrp->freezer.nr_frozen_descendants)
  188. cgroup_update_frozen(cgrp);
  189. spin_unlock_irq(&css_set_lock);
  190. }
  191. /*
  192. * Adjust the task state (freeze or unfreeze) and revisit the state of
  193. * source and destination cgroups.
  194. */
  195. void cgroup_freezer_migrate_task(struct task_struct *task,
  196. struct cgroup *src, struct cgroup *dst)
  197. {
  198. lockdep_assert_held(&css_set_lock);
  199. /*
  200. * Kernel threads are not supposed to be frozen at all.
  201. */
  202. if (task->flags & PF_KTHREAD)
  203. return;
  204. /*
  205. * It's not necessary to do changes if both of the src and dst cgroups
  206. * are not freezing and task is not frozen.
  207. */
  208. if (!test_bit(CGRP_FREEZE, &src->flags) &&
  209. !test_bit(CGRP_FREEZE, &dst->flags) &&
  210. !task->frozen)
  211. return;
  212. /*
  213. * Adjust counters of freezing and frozen tasks.
  214. * Note, that if the task is frozen, but the destination cgroup is not
  215. * frozen, we bump both counters to keep them balanced.
  216. */
  217. if (task->frozen) {
  218. cgroup_inc_frozen_cnt(dst);
  219. cgroup_dec_frozen_cnt(src);
  220. }
  221. cgroup_update_frozen(dst);
  222. cgroup_update_frozen(src);
  223. /*
  224. * Force the task to the desired state.
  225. */
  226. cgroup_freeze_task(task, test_bit(CGRP_FREEZE, &dst->flags));
  227. }
  228. void cgroup_freeze(struct cgroup *cgrp, bool freeze)
  229. {
  230. struct cgroup_subsys_state *css;
  231. struct cgroup *parent;
  232. struct cgroup *dsct;
  233. bool applied = false;
  234. u64 ts_nsec;
  235. bool old_e;
  236. lockdep_assert_held(&cgroup_mutex);
  237. /*
  238. * Nothing changed? Just exit.
  239. */
  240. if (cgrp->freezer.freeze == freeze)
  241. return;
  242. cgrp->freezer.freeze = freeze;
  243. ts_nsec = ktime_get_ns();
  244. /*
  245. * Propagate changes downwards the cgroup tree.
  246. */
  247. css_for_each_descendant_pre(css, &cgrp->self) {
  248. dsct = css->cgroup;
  249. if (cgroup_is_dead(dsct))
  250. continue;
  251. /*
  252. * e_freeze is affected by parent's e_freeze and dst's freeze.
  253. * If old e_freeze eq new e_freeze, no change, its children
  254. * will not be affected. So do nothing and skip the subtree
  255. */
  256. old_e = dsct->freezer.e_freeze;
  257. parent = cgroup_parent(dsct);
  258. dsct->freezer.e_freeze = (dsct->freezer.freeze ||
  259. parent->freezer.e_freeze);
  260. if (dsct->freezer.e_freeze == old_e) {
  261. css = css_rightmost_descendant(css);
  262. continue;
  263. }
  264. /*
  265. * Do change actual state: freeze or unfreeze.
  266. */
  267. cgroup_do_freeze(dsct, freeze, ts_nsec);
  268. applied = true;
  269. }
  270. /*
  271. * Even if the actual state hasn't changed, let's notify a user.
  272. * The state can be enforced by an ancestor cgroup: the cgroup
  273. * can already be in the desired state or it can be locked in the
  274. * opposite state, so that the transition will never happen.
  275. * In both cases it's better to notify a user, that there is
  276. * nothing to wait for.
  277. */
  278. if (!applied) {
  279. TRACE_CGROUP_PATH(notify_frozen, cgrp,
  280. test_bit(CGRP_FROZEN, &cgrp->flags));
  281. cgroup_file_notify(&cgrp->events_file);
  282. }
  283. }