Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[pandora-kernel.git] / kernel / sched_autogroup.c
1 #ifdef CONFIG_SCHED_AUTOGROUP
2
3 #include <linux/proc_fs.h>
4 #include <linux/seq_file.h>
5 #include <linux/kallsyms.h>
6 #include <linux/utsname.h>
7
8 unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
9 static struct autogroup autogroup_default;
10 static atomic_t autogroup_seq_nr;
11
12 static void __init autogroup_init(struct task_struct *init_task)
13 {
14         autogroup_default.tg = &root_task_group;
15         root_task_group.autogroup = &autogroup_default;
16         kref_init(&autogroup_default.kref);
17         init_rwsem(&autogroup_default.lock);
18         init_task->signal->autogroup = &autogroup_default;
19 }
20
21 static inline void autogroup_free(struct task_group *tg)
22 {
23         kfree(tg->autogroup);
24 }
25
26 static inline void autogroup_destroy(struct kref *kref)
27 {
28         struct autogroup *ag = container_of(kref, struct autogroup, kref);
29
30         sched_destroy_group(ag->tg);
31 }
32
33 static inline void autogroup_kref_put(struct autogroup *ag)
34 {
35         kref_put(&ag->kref, autogroup_destroy);
36 }
37
38 static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
39 {
40         kref_get(&ag->kref);
41         return ag;
42 }
43
44 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
45 {
46         struct autogroup *ag;
47         unsigned long flags;
48
49         if (!lock_task_sighand(p, &flags))
50                 return autogroup_kref_get(&autogroup_default);
51
52         ag = autogroup_kref_get(p->signal->autogroup);
53         unlock_task_sighand(p, &flags);
54
55         return ag;
56 }
57
58 static inline struct autogroup *autogroup_create(void)
59 {
60         struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
61         struct task_group *tg;
62
63         if (!ag)
64                 goto out_fail;
65
66         tg = sched_create_group(&root_task_group);
67
68         if (IS_ERR(tg))
69                 goto out_free;
70
71         kref_init(&ag->kref);
72         init_rwsem(&ag->lock);
73         ag->id = atomic_inc_return(&autogroup_seq_nr);
74         ag->tg = tg;
75         tg->autogroup = ag;
76
77         return ag;
78
79 out_free:
80         kfree(ag);
81 out_fail:
82         if (printk_ratelimit()) {
83                 printk(KERN_WARNING "autogroup_create: %s failure.\n",
84                         ag ? "sched_create_group()" : "kmalloc()");
85         }
86
87         return autogroup_kref_get(&autogroup_default);
88 }
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
96         if (p->sched_class != &fair_sched_class)
97                 return false;
98
99         /*
100          * We can only assume the task group can't go away on us if
101          * autogroup_move_group() can see us on ->thread_group list.
102          */
103         if (p->flags & PF_EXITING)
104                 return false;
105
106         return true;
107 }
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
114         if (enabled && task_wants_autogroup(p, tg))
115                 return p->signal->autogroup->tg;
116
117         return tg;
118 }
119
120 static void
121 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
122 {
123         struct autogroup *prev;
124         struct task_struct *t;
125         unsigned long flags;
126
127         BUG_ON(!lock_task_sighand(p, &flags));
128
129         prev = p->signal->autogroup;
130         if (prev == ag) {
131                 unlock_task_sighand(p, &flags);
132                 return;
133         }
134
135         p->signal->autogroup = autogroup_kref_get(ag);
136
137         t = p;
138         do {
139                 sched_move_task(t);
140         } while_each_thread(p, t);
141
142         unlock_task_sighand(p, &flags);
143         autogroup_kref_put(prev);
144 }
145
146 /* Allocates GFP_KERNEL, cannot be called under any spinlock */
147 void sched_autogroup_create_attach(struct task_struct *p)
148 {
149         struct autogroup *ag = autogroup_create();
150
151         autogroup_move_group(p, ag);
152         /* drop extra refrence added by autogroup_create() */
153         autogroup_kref_put(ag);
154 }
155 EXPORT_SYMBOL(sched_autogroup_create_attach);
156
157 /* Cannot be called under siglock.  Currently has no users */
158 void sched_autogroup_detach(struct task_struct *p)
159 {
160         autogroup_move_group(p, &autogroup_default);
161 }
162 EXPORT_SYMBOL(sched_autogroup_detach);
163
164 void sched_autogroup_fork(struct signal_struct *sig)
165 {
166         sig->autogroup = autogroup_task_get(current);
167 }
168
169 void sched_autogroup_exit(struct signal_struct *sig)
170 {
171         autogroup_kref_put(sig->autogroup);
172 }
173
174 static int __init setup_autogroup(char *str)
175 {
176         sysctl_sched_autogroup_enabled = 0;
177
178         return 1;
179 }
180
181 __setup("noautogroup", setup_autogroup);
182
183 #ifdef CONFIG_PROC_FS
184
185 int proc_sched_autogroup_set_nice(struct task_struct *p, int *nice)
186 {
187         static unsigned long next = INITIAL_JIFFIES;
188         struct autogroup *ag;
189         int err;
190
191         if (*nice < -20 || *nice > 19)
192                 return -EINVAL;
193
194         err = security_task_setnice(current, *nice);
195         if (err)
196                 return err;
197
198         if (*nice < 0 && !can_nice(current, *nice))
199                 return -EPERM;
200
201         /* this is a heavy operation taking global locks.. */
202         if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
203                 return -EAGAIN;
204
205         next = HZ / 10 + jiffies;
206         ag = autogroup_task_get(p);
207
208         down_write(&ag->lock);
209         err = sched_group_set_shares(ag->tg, prio_to_weight[*nice + 20]);
210         if (!err)
211                 ag->nice = *nice;
212         up_write(&ag->lock);
213
214         autogroup_kref_put(ag);
215
216         return err;
217 }
218
219 void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
220 {
221         struct autogroup *ag = autogroup_task_get(p);
222
223         down_read(&ag->lock);
224         seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
225         up_read(&ag->lock);
226
227         autogroup_kref_put(ag);
228 }
229 #endif /* CONFIG_PROC_FS */
230
231 #ifdef CONFIG_SCHED_DEBUG
232 static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
233 {
234         return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
235 }
236 #endif /* CONFIG_SCHED_DEBUG */
237
238 #endif /* CONFIG_SCHED_AUTOGROUP */