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