freezer.c 7.8 KB

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