auto_group.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 (tg != &root_task_group)
  92. return false;
  93. if (p->sched_class != &fair_sched_class)
  94. return false;
  95. /*
  96. * We can only assume the task group can't go away on us if
  97. * autogroup_move_group() can see us on ->thread_group list.
  98. */
  99. if (p->flags & PF_EXITING)
  100. return false;
  101. return true;
  102. }
  103. static void
  104. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  105. {
  106. struct autogroup *prev;
  107. struct task_struct *t;
  108. unsigned long flags;
  109. BUG_ON(!lock_task_sighand(p, &flags));
  110. prev = p->signal->autogroup;
  111. if (prev == ag) {
  112. unlock_task_sighand(p, &flags);
  113. return;
  114. }
  115. p->signal->autogroup = autogroup_kref_get(ag);
  116. t = p;
  117. do {
  118. sched_move_task(t);
  119. } while_each_thread(p, t);
  120. unlock_task_sighand(p, &flags);
  121. autogroup_kref_put(prev);
  122. }
  123. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  124. void sched_autogroup_create_attach(struct task_struct *p)
  125. {
  126. struct autogroup *ag = autogroup_create();
  127. autogroup_move_group(p, ag);
  128. /* drop extra reference added by autogroup_create() */
  129. autogroup_kref_put(ag);
  130. }
  131. EXPORT_SYMBOL(sched_autogroup_create_attach);
  132. /* Cannot be called under siglock. Currently has no users */
  133. void sched_autogroup_detach(struct task_struct *p)
  134. {
  135. autogroup_move_group(p, &autogroup_default);
  136. }
  137. EXPORT_SYMBOL(sched_autogroup_detach);
  138. void sched_autogroup_fork(struct signal_struct *sig)
  139. {
  140. sig->autogroup = autogroup_task_get(current);
  141. }
  142. void sched_autogroup_exit(struct signal_struct *sig)
  143. {
  144. autogroup_kref_put(sig->autogroup);
  145. }
  146. static int __init setup_autogroup(char *str)
  147. {
  148. sysctl_sched_autogroup_enabled = 0;
  149. return 1;
  150. }
  151. __setup("noautogroup", setup_autogroup);
  152. #ifdef CONFIG_PROC_FS
  153. int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
  154. {
  155. static unsigned long next = INITIAL_JIFFIES;
  156. struct autogroup *ag;
  157. int err;
  158. if (nice < -20 || nice > 19)
  159. return -EINVAL;
  160. err = security_task_setnice(current, nice);
  161. if (err)
  162. return err;
  163. if (nice < 0 && !can_nice(current, nice))
  164. return -EPERM;
  165. /* this is a heavy operation taking global locks.. */
  166. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  167. return -EAGAIN;
  168. next = HZ / 10 + jiffies;
  169. ag = autogroup_task_get(p);
  170. down_write(&ag->lock);
  171. err = sched_group_set_shares(ag->tg, prio_to_weight[nice + 20]);
  172. if (!err)
  173. ag->nice = nice;
  174. up_write(&ag->lock);
  175. autogroup_kref_put(ag);
  176. return err;
  177. }
  178. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  179. {
  180. struct autogroup *ag = autogroup_task_get(p);
  181. if (!task_group_is_autogroup(ag->tg))
  182. goto out;
  183. down_read(&ag->lock);
  184. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  185. up_read(&ag->lock);
  186. out:
  187. autogroup_kref_put(ag);
  188. }
  189. #endif /* CONFIG_PROC_FS */
  190. #ifdef CONFIG_SCHED_DEBUG
  191. int autogroup_path(struct task_group *tg, char *buf, int buflen)
  192. {
  193. if (!task_group_is_autogroup(tg))
  194. return 0;
  195. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  196. }
  197. #endif /* CONFIG_SCHED_DEBUG */
  198. #endif /* CONFIG_SCHED_AUTOGROUP */