freezer.c 7.9 KB

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