sched_autogroup.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifdef CONFIG_SCHED_AUTOGROUP
  2. #include <linux/proc_fs.h>
  3. #include <linux/seq_file.h>
  4. #include <linux/kallsyms.h>
  5. #include <linux/utsname.h>
  6. unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
  7. static struct autogroup autogroup_default;
  8. static atomic_t autogroup_seq_nr;
  9. static void __init autogroup_init(struct task_struct *init_task)
  10. {
  11. autogroup_default.tg = &root_task_group;
  12. kref_init(&autogroup_default.kref);
  13. init_rwsem(&autogroup_default.lock);
  14. init_task->signal->autogroup = &autogroup_default;
  15. }
  16. static inline void autogroup_free(struct task_group *tg)
  17. {
  18. kfree(tg->autogroup);
  19. }
  20. static inline void autogroup_destroy(struct kref *kref)
  21. {
  22. struct autogroup *ag = container_of(kref, struct autogroup, kref);
  23. #ifdef CONFIG_RT_GROUP_SCHED
  24. /* We've redirected RT tasks to the root task group... */
  25. ag->tg->rt_se = NULL;
  26. ag->tg->rt_rq = NULL;
  27. #endif
  28. sched_destroy_group(ag->tg);
  29. }
  30. static inline void autogroup_kref_put(struct autogroup *ag)
  31. {
  32. kref_put(&ag->kref, autogroup_destroy);
  33. }
  34. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  35. {
  36. kref_get(&ag->kref);
  37. return ag;
  38. }
  39. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  40. {
  41. struct autogroup *ag;
  42. unsigned long flags;
  43. if (!lock_task_sighand(p, &flags))
  44. return autogroup_kref_get(&autogroup_default);
  45. ag = autogroup_kref_get(p->signal->autogroup);
  46. unlock_task_sighand(p, &flags);
  47. return ag;
  48. }
  49. #ifdef CONFIG_RT_GROUP_SCHED
  50. static void free_rt_sched_group(struct task_group *tg);
  51. #endif
  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. static inline bool
  90. task_wants_autogroup(struct task_struct *p, struct task_group *tg)
  91. {
  92. if (tg != &root_task_group)
  93. return false;
  94. if (p->sched_class != &fair_sched_class)
  95. return false;
  96. /*
  97. * We can only assume the task group can't go away on us if
  98. * autogroup_move_group() can see us on ->thread_group list.
  99. */
  100. if (p->flags & PF_EXITING)
  101. return false;
  102. return true;
  103. }
  104. static inline bool task_group_is_autogroup(struct task_group *tg)
  105. {
  106. return !!tg->autogroup;
  107. }
  108. static inline struct task_group *
  109. autogroup_task_group(struct task_struct *p, struct task_group *tg)
  110. {
  111. int enabled = ACCESS_ONCE(sysctl_sched_autogroup_enabled);
  112. if (enabled && task_wants_autogroup(p, tg))
  113. return p->signal->autogroup->tg;
  114. return tg;
  115. }
  116. static void
  117. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  118. {
  119. struct autogroup *prev;
  120. struct task_struct *t;
  121. unsigned long flags;
  122. BUG_ON(!lock_task_sighand(p, &flags));
  123. prev = p->signal->autogroup;
  124. if (prev == ag) {
  125. unlock_task_sighand(p, &flags);
  126. return;
  127. }
  128. p->signal->autogroup = autogroup_kref_get(ag);
  129. if (!ACCESS_ONCE(sysctl_sched_autogroup_enabled))
  130. goto out;
  131. t = p;
  132. do {
  133. sched_move_task(t);
  134. } while_each_thread(p, t);
  135. out:
  136. unlock_task_sighand(p, &flags);
  137. autogroup_kref_put(prev);
  138. }
  139. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  140. void sched_autogroup_create_attach(struct task_struct *p)
  141. {
  142. struct autogroup *ag = autogroup_create();
  143. autogroup_move_group(p, ag);
  144. /* drop extra reference added by autogroup_create() */
  145. autogroup_kref_put(ag);
  146. }
  147. EXPORT_SYMBOL(sched_autogroup_create_attach);
  148. /* Cannot be called under siglock. Currently has no users */
  149. void sched_autogroup_detach(struct task_struct *p)
  150. {
  151. autogroup_move_group(p, &autogroup_default);
  152. }
  153. EXPORT_SYMBOL(sched_autogroup_detach);
  154. void sched_autogroup_fork(struct signal_struct *sig)
  155. {
  156. sig->autogroup = autogroup_task_get(current);
  157. }
  158. void sched_autogroup_exit(struct signal_struct *sig)
  159. {
  160. autogroup_kref_put(sig->autogroup);
  161. }
  162. static int __init setup_autogroup(char *str)
  163. {
  164. sysctl_sched_autogroup_enabled = 0;
  165. return 1;
  166. }
  167. __setup("noautogroup", setup_autogroup);
  168. #ifdef CONFIG_PROC_FS
  169. int proc_sched_autogroup_set_nice(struct task_struct *p, int *nice)
  170. {
  171. static unsigned long next = INITIAL_JIFFIES;
  172. struct autogroup *ag;
  173. int err;
  174. if (*nice < -20 || *nice > 19)
  175. return -EINVAL;
  176. err = security_task_setnice(current, *nice);
  177. if (err)
  178. return err;
  179. if (*nice < 0 && !can_nice(current, *nice))
  180. return -EPERM;
  181. /* this is a heavy operation taking global locks.. */
  182. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  183. return -EAGAIN;
  184. next = HZ / 10 + jiffies;
  185. ag = autogroup_task_get(p);
  186. down_write(&ag->lock);
  187. err = sched_group_set_shares(ag->tg, prio_to_weight[*nice + 20]);
  188. if (!err)
  189. ag->nice = *nice;
  190. up_write(&ag->lock);
  191. autogroup_kref_put(ag);
  192. return err;
  193. }
  194. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  195. {
  196. struct autogroup *ag = autogroup_task_get(p);
  197. if (!task_group_is_autogroup(ag->tg))
  198. goto out;
  199. down_read(&ag->lock);
  200. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  201. up_read(&ag->lock);
  202. out:
  203. autogroup_kref_put(ag);
  204. }
  205. #endif /* CONFIG_PROC_FS */
  206. #ifdef CONFIG_SCHED_DEBUG
  207. static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
  208. {
  209. if (!task_group_is_autogroup(tg))
  210. return 0;
  211. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  212. }
  213. #endif /* CONFIG_SCHED_DEBUG */
  214. #endif /* CONFIG_SCHED_AUTOGROUP */