auto_group.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifdef CONFIG_SCHED_AUTOGROUP
  2. #include "sched.h"
  3. #include <linux/proc_fs.h>
  4. #include <linux/seq_file.h>
  5. #include <linux/kallsyms.h>
  6. #include <linux/utsname.h>
  7. #include <linux/security.h>
  8. #include <linux/export.h>
  9. unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
  10. static struct autogroup autogroup_default;
  11. static atomic_t autogroup_seq_nr;
  12. void __init autogroup_init(struct task_struct *init_task)
  13. {
  14. autogroup_default.tg = &root_task_group;
  15. kref_init(&autogroup_default.kref);
  16. init_rwsem(&autogroup_default.lock);
  17. init_task->signal->autogroup = &autogroup_default;
  18. }
  19. void autogroup_free(struct task_group *tg)
  20. {
  21. kfree(tg->autogroup);
  22. }
  23. static inline void autogroup_destroy(struct kref *kref)
  24. {
  25. struct autogroup *ag = container_of(kref, struct autogroup, kref);
  26. #ifdef CONFIG_RT_GROUP_SCHED
  27. /* We've redirected RT tasks to the root task group... */
  28. ag->tg->rt_se = NULL;
  29. ag->tg->rt_rq = NULL;
  30. #endif
  31. sched_destroy_group(ag->tg);
  32. }
  33. static inline void autogroup_kref_put(struct autogroup *ag)
  34. {
  35. kref_put(&ag->kref, autogroup_destroy);
  36. }
  37. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  38. {
  39. kref_get(&ag->kref);
  40. return ag;
  41. }
  42. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  43. {
  44. struct autogroup *ag;
  45. unsigned long flags;
  46. if (!lock_task_sighand(p, &flags))
  47. return autogroup_kref_get(&autogroup_default);
  48. ag = autogroup_kref_get(p->signal->autogroup);
  49. unlock_task_sighand(p, &flags);
  50. return ag;
  51. }
  52. static inline struct autogroup *autogroup_create(void)
  53. {
  54. struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
  55. struct task_group *tg;
  56. if (!ag)
  57. goto out_fail;
  58. tg = sched_create_group(&root_task_group);
  59. if (IS_ERR(tg))
  60. goto out_free;
  61. kref_init(&ag->kref);
  62. init_rwsem(&ag->lock);
  63. ag->id = atomic_inc_return(&autogroup_seq_nr);
  64. ag->tg = tg;
  65. #ifdef CONFIG_RT_GROUP_SCHED
  66. /*
  67. * Autogroup RT tasks are redirected to the root task group
  68. * so we don't have to move tasks around upon policy change,
  69. * or flail around trying to allocate bandwidth on the fly.
  70. * A bandwidth exception in __sched_setscheduler() allows
  71. * the policy change to proceed. Thereafter, task_group()
  72. * returns &root_task_group, so zero bandwidth is required.
  73. */
  74. free_rt_sched_group(tg);
  75. tg->rt_se = root_task_group.rt_se;
  76. tg->rt_rq = root_task_group.rt_rq;
  77. #endif
  78. tg->autogroup = ag;
  79. return ag;
  80. out_free:
  81. kfree(ag);
  82. out_fail:
  83. if (printk_ratelimit()) {
  84. printk(KERN_WARNING "autogroup_create: %s failure.\n",
  85. ag ? "sched_create_group()" : "kmalloc()");
  86. }
  87. return autogroup_kref_get(&autogroup_default);
  88. }
  89. bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
  90. {
  91. if (!sysctl_sched_autogroup_enabled)
  92. return false;
  93. if (tg != &root_task_group)
  94. return false;
  95. if (p->sched_class != &fair_sched_class)
  96. return false;
  97. /*
  98. * We can only assume the task group can't go away on us if
  99. * autogroup_move_group() can see us on ->thread_group list.
  100. */
  101. if (p->flags & PF_EXITING)
  102. return false;
  103. return true;
  104. }
  105. static void
  106. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  107. {
  108. struct autogroup *prev;
  109. struct task_struct *t;
  110. unsigned long flags;
  111. BUG_ON(!lock_task_sighand(p, &flags));
  112. prev = p->signal->autogroup;
  113. if (prev == ag) {
  114. unlock_task_sighand(p, &flags);
  115. return;
  116. }
  117. p->signal->autogroup = autogroup_kref_get(ag);
  118. t = p;
  119. do {
  120. sched_move_task(t);
  121. } while_each_thread(p, t);
  122. unlock_task_sighand(p, &flags);
  123. autogroup_kref_put(prev);
  124. }
  125. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  126. void sched_autogroup_create_attach(struct task_struct *p)
  127. {
  128. struct autogroup *ag;
  129. if (!sysctl_sched_autogroup_enabled)
  130. return;
  131. ag = autogroup_create();
  132. autogroup_move_group(p, ag);
  133. /* drop extra reference added by autogroup_create() */
  134. autogroup_kref_put(ag);
  135. }
  136. EXPORT_SYMBOL(sched_autogroup_create_attach);
  137. /* Cannot be called under siglock. Currently has no users */
  138. void sched_autogroup_detach(struct task_struct *p)
  139. {
  140. autogroup_move_group(p, &autogroup_default);
  141. }
  142. EXPORT_SYMBOL(sched_autogroup_detach);
  143. void sched_autogroup_fork(struct signal_struct *sig)
  144. {
  145. if (!sysctl_sched_autogroup_enabled)
  146. return;
  147. sig->autogroup = autogroup_task_get(current);
  148. }
  149. void sched_autogroup_exit(struct signal_struct *sig)
  150. {
  151. if (!sysctl_sched_autogroup_enabled)
  152. return;
  153. autogroup_kref_put(sig->autogroup);
  154. }
  155. static int __init setup_autogroup(char *str)
  156. {
  157. sysctl_sched_autogroup_enabled = 0;
  158. return 1;
  159. }
  160. __setup("noautogroup", setup_autogroup);
  161. #ifdef CONFIG_SCHED_DEBUG
  162. int autogroup_path(struct task_group *tg, char *buf, int buflen)
  163. {
  164. if (!task_group_is_autogroup(tg))
  165. return 0;
  166. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  167. }
  168. #endif /* CONFIG_SCHED_DEBUG */
  169. #endif /* CONFIG_SCHED_AUTOGROUP */