auto_group.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_offline_group(ag->tg);
  32. sched_destroy_group(ag->tg);
  33. }
  34. static inline void autogroup_kref_put(struct autogroup *ag)
  35. {
  36. kref_put(&ag->kref, autogroup_destroy);
  37. }
  38. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  39. {
  40. kref_get(&ag->kref);
  41. return ag;
  42. }
  43. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  44. {
  45. struct autogroup *ag;
  46. unsigned long flags;
  47. if (!lock_task_sighand(p, &flags))
  48. return autogroup_kref_get(&autogroup_default);
  49. ag = autogroup_kref_get(p->signal->autogroup);
  50. unlock_task_sighand(p, &flags);
  51. return ag;
  52. }
  53. static inline struct autogroup *autogroup_create(void)
  54. {
  55. struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
  56. struct task_group *tg;
  57. if (!ag)
  58. goto out_fail;
  59. tg = sched_create_group(&root_task_group);
  60. if (IS_ERR(tg))
  61. goto out_free;
  62. sched_online_group(tg, &root_task_group);
  63. kref_init(&ag->kref);
  64. init_rwsem(&ag->lock);
  65. ag->id = atomic_inc_return(&autogroup_seq_nr);
  66. ag->tg = tg;
  67. #ifdef CONFIG_RT_GROUP_SCHED
  68. /*
  69. * Autogroup RT tasks are redirected to the root task group
  70. * so we don't have to move tasks around upon policy change,
  71. * or flail around trying to allocate bandwidth on the fly.
  72. * A bandwidth exception in __sched_setscheduler() allows
  73. * the policy change to proceed. Thereafter, task_group()
  74. * returns &root_task_group, so zero bandwidth is required.
  75. */
  76. free_rt_sched_group(tg);
  77. tg->rt_se = root_task_group.rt_se;
  78. tg->rt_rq = root_task_group.rt_rq;
  79. #endif
  80. tg->autogroup = ag;
  81. return ag;
  82. out_free:
  83. kfree(ag);
  84. out_fail:
  85. if (printk_ratelimit()) {
  86. printk(KERN_WARNING "autogroup_create: %s failure.\n",
  87. ag ? "sched_create_group()" : "kmalloc()");
  88. }
  89. return autogroup_kref_get(&autogroup_default);
  90. }
  91. bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
  92. {
  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. if (!ACCESS_ONCE(sysctl_sched_autogroup_enabled))
  119. goto out;
  120. t = p;
  121. do {
  122. sched_move_task(t);
  123. } while_each_thread(p, t);
  124. out:
  125. unlock_task_sighand(p, &flags);
  126. autogroup_kref_put(prev);
  127. }
  128. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  129. void sched_autogroup_create_attach(struct task_struct *p)
  130. {
  131. struct autogroup *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. sig->autogroup = autogroup_task_get(current);
  146. }
  147. void sched_autogroup_exit(struct signal_struct *sig)
  148. {
  149. autogroup_kref_put(sig->autogroup);
  150. }
  151. static int __init setup_autogroup(char *str)
  152. {
  153. sysctl_sched_autogroup_enabled = 0;
  154. return 1;
  155. }
  156. __setup("noautogroup", setup_autogroup);
  157. #ifdef CONFIG_PROC_FS
  158. int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
  159. {
  160. static unsigned long next = INITIAL_JIFFIES;
  161. struct autogroup *ag;
  162. int err;
  163. if (nice < -20 || nice > 19)
  164. return -EINVAL;
  165. err = security_task_setnice(current, nice);
  166. if (err)
  167. return err;
  168. if (nice < 0 && !can_nice(current, nice))
  169. return -EPERM;
  170. /* this is a heavy operation taking global locks.. */
  171. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  172. return -EAGAIN;
  173. next = HZ / 10 + jiffies;
  174. ag = autogroup_task_get(p);
  175. down_write(&ag->lock);
  176. err = sched_group_set_shares(ag->tg, prio_to_weight[nice + 20]);
  177. if (!err)
  178. ag->nice = nice;
  179. up_write(&ag->lock);
  180. autogroup_kref_put(ag);
  181. return err;
  182. }
  183. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  184. {
  185. struct autogroup *ag = autogroup_task_get(p);
  186. if (!task_group_is_autogroup(ag->tg))
  187. goto out;
  188. down_read(&ag->lock);
  189. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  190. up_read(&ag->lock);
  191. out:
  192. autogroup_kref_put(ag);
  193. }
  194. #endif /* CONFIG_PROC_FS */
  195. #ifdef CONFIG_SCHED_DEBUG
  196. int autogroup_path(struct task_group *tg, char *buf, int buflen)
  197. {
  198. if (!task_group_is_autogroup(tg))
  199. return 0;
  200. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  201. }
  202. #endif /* CONFIG_SCHED_DEBUG */
  203. #endif /* CONFIG_SCHED_AUTOGROUP */