sched_autogroup.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. sched_destroy_group(ag->tg);
  25. }
  26. static inline void autogroup_kref_put(struct autogroup *ag)
  27. {
  28. kref_put(&ag->kref, autogroup_destroy);
  29. }
  30. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  31. {
  32. kref_get(&ag->kref);
  33. return ag;
  34. }
  35. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  36. {
  37. struct autogroup *ag;
  38. unsigned long flags;
  39. if (!lock_task_sighand(p, &flags))
  40. return autogroup_kref_get(&autogroup_default);
  41. ag = autogroup_kref_get(p->signal->autogroup);
  42. unlock_task_sighand(p, &flags);
  43. return ag;
  44. }
  45. static inline struct autogroup *autogroup_create(void)
  46. {
  47. struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
  48. struct task_group *tg;
  49. if (!ag)
  50. goto out_fail;
  51. tg = sched_create_group(&root_task_group);
  52. if (IS_ERR(tg))
  53. goto out_free;
  54. kref_init(&ag->kref);
  55. init_rwsem(&ag->lock);
  56. ag->id = atomic_inc_return(&autogroup_seq_nr);
  57. ag->tg = tg;
  58. tg->autogroup = ag;
  59. return ag;
  60. out_free:
  61. kfree(ag);
  62. out_fail:
  63. if (printk_ratelimit()) {
  64. printk(KERN_WARNING "autogroup_create: %s failure.\n",
  65. ag ? "sched_create_group()" : "kmalloc()");
  66. }
  67. return autogroup_kref_get(&autogroup_default);
  68. }
  69. static inline bool
  70. task_wants_autogroup(struct task_struct *p, struct task_group *tg)
  71. {
  72. if (tg != &root_task_group)
  73. return false;
  74. if (p->sched_class != &fair_sched_class)
  75. return false;
  76. /*
  77. * We can only assume the task group can't go away on us if
  78. * autogroup_move_group() can see us on ->thread_group list.
  79. */
  80. if (p->flags & PF_EXITING)
  81. return false;
  82. return true;
  83. }
  84. static inline struct task_group *
  85. autogroup_task_group(struct task_struct *p, struct task_group *tg)
  86. {
  87. int enabled = ACCESS_ONCE(sysctl_sched_autogroup_enabled);
  88. if (enabled && task_wants_autogroup(p, tg))
  89. return p->signal->autogroup->tg;
  90. return tg;
  91. }
  92. static void
  93. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  94. {
  95. struct autogroup *prev;
  96. struct task_struct *t;
  97. unsigned long flags;
  98. BUG_ON(!lock_task_sighand(p, &flags));
  99. prev = p->signal->autogroup;
  100. if (prev == ag) {
  101. unlock_task_sighand(p, &flags);
  102. return;
  103. }
  104. p->signal->autogroup = autogroup_kref_get(ag);
  105. t = p;
  106. do {
  107. sched_move_task(t);
  108. } while_each_thread(p, t);
  109. unlock_task_sighand(p, &flags);
  110. autogroup_kref_put(prev);
  111. }
  112. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  113. void sched_autogroup_create_attach(struct task_struct *p)
  114. {
  115. struct autogroup *ag = autogroup_create();
  116. autogroup_move_group(p, ag);
  117. /* drop extra refrence added by autogroup_create() */
  118. autogroup_kref_put(ag);
  119. }
  120. EXPORT_SYMBOL(sched_autogroup_create_attach);
  121. /* Cannot be called under siglock. Currently has no users */
  122. void sched_autogroup_detach(struct task_struct *p)
  123. {
  124. autogroup_move_group(p, &autogroup_default);
  125. }
  126. EXPORT_SYMBOL(sched_autogroup_detach);
  127. void sched_autogroup_fork(struct signal_struct *sig)
  128. {
  129. sig->autogroup = autogroup_task_get(current);
  130. }
  131. void sched_autogroup_exit(struct signal_struct *sig)
  132. {
  133. autogroup_kref_put(sig->autogroup);
  134. }
  135. static int __init setup_autogroup(char *str)
  136. {
  137. sysctl_sched_autogroup_enabled = 0;
  138. return 1;
  139. }
  140. __setup("noautogroup", setup_autogroup);
  141. #ifdef CONFIG_PROC_FS
  142. int proc_sched_autogroup_set_nice(struct task_struct *p, int *nice)
  143. {
  144. static unsigned long next = INITIAL_JIFFIES;
  145. struct autogroup *ag;
  146. int err;
  147. if (*nice < -20 || *nice > 19)
  148. return -EINVAL;
  149. err = security_task_setnice(current, *nice);
  150. if (err)
  151. return err;
  152. if (*nice < 0 && !can_nice(current, *nice))
  153. return -EPERM;
  154. /* this is a heavy operation taking global locks.. */
  155. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  156. return -EAGAIN;
  157. next = HZ / 10 + jiffies;
  158. ag = autogroup_task_get(p);
  159. down_write(&ag->lock);
  160. err = sched_group_set_shares(ag->tg, prio_to_weight[*nice + 20]);
  161. if (!err)
  162. ag->nice = *nice;
  163. up_write(&ag->lock);
  164. autogroup_kref_put(ag);
  165. return err;
  166. }
  167. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  168. {
  169. struct autogroup *ag = autogroup_task_get(p);
  170. down_read(&ag->lock);
  171. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  172. up_read(&ag->lock);
  173. autogroup_kref_put(ag);
  174. }
  175. #endif /* CONFIG_PROC_FS */
  176. #ifdef CONFIG_SCHED_DEBUG
  177. static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
  178. {
  179. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  180. }
  181. #endif /* CONFIG_SCHED_DEBUG */
  182. #endif /* CONFIG_SCHED_AUTOGROUP */