sched_autogroup.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. root_task_group.autogroup = &autogroup_default;
  13. kref_init(&autogroup_default.kref);
  14. init_rwsem(&autogroup_default.lock);
  15. init_task->signal->autogroup = &autogroup_default;
  16. }
  17. static inline void autogroup_free(struct task_group *tg)
  18. {
  19. kfree(tg->autogroup);
  20. }
  21. static inline void autogroup_destroy(struct kref *kref)
  22. {
  23. struct autogroup *ag = container_of(kref, struct autogroup, kref);
  24. #ifdef CONFIG_RT_GROUP_SCHED
  25. /* We've redirected RT tasks to the root task group... */
  26. ag->tg->rt_se = NULL;
  27. ag->tg->rt_rq = NULL;
  28. #endif
  29. sched_destroy_group(ag->tg);
  30. }
  31. static inline void autogroup_kref_put(struct autogroup *ag)
  32. {
  33. kref_put(&ag->kref, autogroup_destroy);
  34. }
  35. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  36. {
  37. kref_get(&ag->kref);
  38. return ag;
  39. }
  40. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  41. {
  42. struct autogroup *ag;
  43. unsigned long flags;
  44. if (!lock_task_sighand(p, &flags))
  45. return autogroup_kref_get(&autogroup_default);
  46. ag = autogroup_kref_get(p->signal->autogroup);
  47. unlock_task_sighand(p, &flags);
  48. return ag;
  49. }
  50. #ifdef CONFIG_RT_GROUP_SCHED
  51. static void free_rt_sched_group(struct task_group *tg);
  52. #endif
  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. kref_init(&ag->kref);
  63. init_rwsem(&ag->lock);
  64. ag->id = atomic_inc_return(&autogroup_seq_nr);
  65. ag->tg = tg;
  66. #ifdef CONFIG_RT_GROUP_SCHED
  67. /*
  68. * Autogroup RT tasks are redirected to the root task group
  69. * so we don't have to move tasks around upon policy change,
  70. * or flail around trying to allocate bandwidth on the fly.
  71. * A bandwidth exception in __sched_setscheduler() allows
  72. * the policy change to proceed. Thereafter, task_group()
  73. * returns &root_task_group, so zero bandwidth is required.
  74. */
  75. free_rt_sched_group(tg);
  76. tg->rt_se = root_task_group.rt_se;
  77. tg->rt_rq = root_task_group.rt_rq;
  78. #endif
  79. tg->autogroup = ag;
  80. return ag;
  81. out_free:
  82. kfree(ag);
  83. out_fail:
  84. if (printk_ratelimit()) {
  85. printk(KERN_WARNING "autogroup_create: %s failure.\n",
  86. ag ? "sched_create_group()" : "kmalloc()");
  87. }
  88. return autogroup_kref_get(&autogroup_default);
  89. }
  90. static inline bool
  91. 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 inline bool task_group_is_autogroup(struct task_group *tg)
  106. {
  107. return tg != &root_task_group && tg->autogroup;
  108. }
  109. static inline struct task_group *
  110. autogroup_task_group(struct task_struct *p, struct task_group *tg)
  111. {
  112. int enabled = ACCESS_ONCE(sysctl_sched_autogroup_enabled);
  113. if (enabled && task_wants_autogroup(p, tg))
  114. return p->signal->autogroup->tg;
  115. return tg;
  116. }
  117. static void
  118. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  119. {
  120. struct autogroup *prev;
  121. struct task_struct *t;
  122. unsigned long flags;
  123. BUG_ON(!lock_task_sighand(p, &flags));
  124. prev = p->signal->autogroup;
  125. if (prev == ag) {
  126. unlock_task_sighand(p, &flags);
  127. return;
  128. }
  129. p->signal->autogroup = autogroup_kref_get(ag);
  130. t = p;
  131. do {
  132. sched_move_task(t);
  133. } while_each_thread(p, t);
  134. unlock_task_sighand(p, &flags);
  135. autogroup_kref_put(prev);
  136. }
  137. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  138. void sched_autogroup_create_attach(struct task_struct *p)
  139. {
  140. struct autogroup *ag = autogroup_create();
  141. autogroup_move_group(p, ag);
  142. /* drop extra refrence added by autogroup_create() */
  143. autogroup_kref_put(ag);
  144. }
  145. EXPORT_SYMBOL(sched_autogroup_create_attach);
  146. /* Cannot be called under siglock. Currently has no users */
  147. void sched_autogroup_detach(struct task_struct *p)
  148. {
  149. autogroup_move_group(p, &autogroup_default);
  150. }
  151. EXPORT_SYMBOL(sched_autogroup_detach);
  152. void sched_autogroup_fork(struct signal_struct *sig)
  153. {
  154. sig->autogroup = autogroup_task_get(current);
  155. }
  156. void sched_autogroup_exit(struct signal_struct *sig)
  157. {
  158. autogroup_kref_put(sig->autogroup);
  159. }
  160. static int __init setup_autogroup(char *str)
  161. {
  162. sysctl_sched_autogroup_enabled = 0;
  163. return 1;
  164. }
  165. __setup("noautogroup", setup_autogroup);
  166. #ifdef CONFIG_PROC_FS
  167. int proc_sched_autogroup_set_nice(struct task_struct *p, int *nice)
  168. {
  169. static unsigned long next = INITIAL_JIFFIES;
  170. struct autogroup *ag;
  171. int err;
  172. if (*nice < -20 || *nice > 19)
  173. return -EINVAL;
  174. err = security_task_setnice(current, *nice);
  175. if (err)
  176. return err;
  177. if (*nice < 0 && !can_nice(current, *nice))
  178. return -EPERM;
  179. /* this is a heavy operation taking global locks.. */
  180. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  181. return -EAGAIN;
  182. next = HZ / 10 + jiffies;
  183. ag = autogroup_task_get(p);
  184. down_write(&ag->lock);
  185. err = sched_group_set_shares(ag->tg, prio_to_weight[*nice + 20]);
  186. if (!err)
  187. ag->nice = *nice;
  188. up_write(&ag->lock);
  189. autogroup_kref_put(ag);
  190. return err;
  191. }
  192. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  193. {
  194. struct autogroup *ag = autogroup_task_get(p);
  195. down_read(&ag->lock);
  196. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  197. up_read(&ag->lock);
  198. autogroup_kref_put(ag);
  199. }
  200. #endif /* CONFIG_PROC_FS */
  201. #ifdef CONFIG_SCHED_DEBUG
  202. static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
  203. {
  204. int enabled = ACCESS_ONCE(sysctl_sched_autogroup_enabled);
  205. if (!enabled || !tg->autogroup)
  206. return 0;
  207. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  208. }
  209. #endif /* CONFIG_SCHED_DEBUG */
  210. #endif /* CONFIG_SCHED_AUTOGROUP */