auto_group.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "sched.h"
  2. #include <linux/proc_fs.h>
  3. #include <linux/seq_file.h>
  4. #include <linux/kallsyms.h>
  5. #include <linux/utsname.h>
  6. #include <linux/security.h>
  7. #include <linux/export.h>
  8. unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
  9. static struct autogroup autogroup_default;
  10. static atomic_t autogroup_seq_nr;
  11. void __init autogroup_init(struct task_struct *init_task)
  12. {
  13. autogroup_default.tg = &root_task_group;
  14. kref_init(&autogroup_default.kref);
  15. init_rwsem(&autogroup_default.lock);
  16. init_task->signal->autogroup = &autogroup_default;
  17. }
  18. void autogroup_free(struct task_group *tg)
  19. {
  20. kfree(tg->autogroup);
  21. }
  22. static inline void autogroup_destroy(struct kref *kref)
  23. {
  24. struct autogroup *ag = container_of(kref, struct autogroup, kref);
  25. #ifdef CONFIG_RT_GROUP_SCHED
  26. /* We've redirected RT tasks to the root task group... */
  27. ag->tg->rt_se = NULL;
  28. ag->tg->rt_rq = NULL;
  29. #endif
  30. sched_offline_group(ag->tg);
  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.
  72. */
  73. free_rt_sched_group(tg);
  74. tg->rt_se = root_task_group.rt_se;
  75. tg->rt_rq = root_task_group.rt_rq;
  76. #endif
  77. tg->autogroup = ag;
  78. sched_online_group(tg, &root_task_group);
  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. /*
  94. * If we race with autogroup_move_group() the caller can use the old
  95. * value of signal->autogroup but in this case sched_move_task() will
  96. * be called again before autogroup_kref_put().
  97. */
  98. return true;
  99. }
  100. static void
  101. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  102. {
  103. struct autogroup *prev;
  104. struct task_struct *t;
  105. unsigned long flags;
  106. BUG_ON(!lock_task_sighand(p, &flags));
  107. prev = p->signal->autogroup;
  108. if (prev == ag) {
  109. unlock_task_sighand(p, &flags);
  110. return;
  111. }
  112. p->signal->autogroup = autogroup_kref_get(ag);
  113. /*
  114. * We can't avoid sched_move_task() after we changed signal->autogroup,
  115. * this process can already run with task_group() == prev->tg or we can
  116. * race with cgroup code which can read autogroup = prev under rq->lock.
  117. * In the latter case for_each_thread() can not miss a migrating thread,
  118. * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
  119. * can't be removed from thread list, we hold ->siglock.
  120. */
  121. for_each_thread(p, t)
  122. sched_move_task(t);
  123. unlock_task_sighand(p, &flags);
  124. autogroup_kref_put(prev);
  125. }
  126. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  127. void sched_autogroup_create_attach(struct task_struct *p)
  128. {
  129. struct autogroup *ag = autogroup_create();
  130. autogroup_move_group(p, ag);
  131. /* drop extra reference added by autogroup_create() */
  132. autogroup_kref_put(ag);
  133. }
  134. EXPORT_SYMBOL(sched_autogroup_create_attach);
  135. /* Cannot be called under siglock. Currently has no users */
  136. void sched_autogroup_detach(struct task_struct *p)
  137. {
  138. autogroup_move_group(p, &autogroup_default);
  139. }
  140. EXPORT_SYMBOL(sched_autogroup_detach);
  141. void sched_autogroup_fork(struct signal_struct *sig)
  142. {
  143. sig->autogroup = autogroup_task_get(current);
  144. }
  145. void sched_autogroup_exit(struct signal_struct *sig)
  146. {
  147. autogroup_kref_put(sig->autogroup);
  148. }
  149. static int __init setup_autogroup(char *str)
  150. {
  151. sysctl_sched_autogroup_enabled = 0;
  152. return 1;
  153. }
  154. __setup("noautogroup", setup_autogroup);
  155. #ifdef CONFIG_PROC_FS
  156. int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
  157. {
  158. static unsigned long next = INITIAL_JIFFIES;
  159. struct autogroup *ag;
  160. int err;
  161. if (nice < MIN_NICE || nice > MAX_NICE)
  162. return -EINVAL;
  163. err = security_task_setnice(current, nice);
  164. if (err)
  165. return err;
  166. if (nice < 0 && !can_nice(current, nice))
  167. return -EPERM;
  168. /* this is a heavy operation taking global locks.. */
  169. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  170. return -EAGAIN;
  171. next = HZ / 10 + jiffies;
  172. ag = autogroup_task_get(p);
  173. down_write(&ag->lock);
  174. err = sched_group_set_shares(ag->tg, prio_to_weight[nice + 20]);
  175. if (!err)
  176. ag->nice = nice;
  177. up_write(&ag->lock);
  178. autogroup_kref_put(ag);
  179. return err;
  180. }
  181. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  182. {
  183. struct autogroup *ag = autogroup_task_get(p);
  184. if (!task_group_is_autogroup(ag->tg))
  185. goto out;
  186. down_read(&ag->lock);
  187. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  188. up_read(&ag->lock);
  189. out:
  190. autogroup_kref_put(ag);
  191. }
  192. #endif /* CONFIG_PROC_FS */
  193. #ifdef CONFIG_SCHED_DEBUG
  194. int autogroup_path(struct task_group *tg, char *buf, int buflen)
  195. {
  196. if (!task_group_is_autogroup(tg))
  197. return 0;
  198. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  199. }
  200. #endif /* CONFIG_SCHED_DEBUG */