memcg: add lock to synchronize page accounting and migration
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / sched_autogroup.c
blob32a723b8f84cac397c51579a1f3099d35d1a25f0
1 #ifdef CONFIG_SCHED_AUTOGROUP
3 #include <linux/proc_fs.h>
4 #include <linux/seq_file.h>
5 #include <linux/kallsyms.h>
6 #include <linux/utsname.h>
8 unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
9 static struct autogroup autogroup_default;
10 static atomic_t autogroup_seq_nr;
12 static void __init autogroup_init(struct task_struct *init_task)
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;
21 static inline void autogroup_free(struct task_group *tg)
23 kfree(tg->autogroup);
26 static inline void autogroup_destroy(struct kref *kref)
28 struct autogroup *ag = container_of(kref, struct autogroup, kref);
30 sched_destroy_group(ag->tg);
33 static inline void autogroup_kref_put(struct autogroup *ag)
35 kref_put(&ag->kref, autogroup_destroy);
38 static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
40 kref_get(&ag->kref);
41 return ag;
44 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
46 struct autogroup *ag;
47 unsigned long flags;
49 if (!lock_task_sighand(p, &flags))
50 return autogroup_kref_get(&autogroup_default);
52 ag = autogroup_kref_get(p->signal->autogroup);
53 unlock_task_sighand(p, &flags);
55 return ag;
58 static inline struct autogroup *autogroup_create(void)
60 struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
61 struct task_group *tg;
63 if (!ag)
64 goto out_fail;
66 tg = sched_create_group(&root_task_group);
68 if (IS_ERR(tg))
69 goto out_free;
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;
77 return ag;
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()");
87 return autogroup_kref_get(&autogroup_default);
90 static inline bool
91 task_wants_autogroup(struct task_struct *p, struct task_group *tg)
93 if (tg != &root_task_group)
94 return false;
96 if (p->sched_class != &fair_sched_class)
97 return false;
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.
103 if (p->flags & PF_EXITING)
104 return false;
106 return true;
109 static inline struct task_group *
110 autogroup_task_group(struct task_struct *p, struct task_group *tg)
112 int enabled = ACCESS_ONCE(sysctl_sched_autogroup_enabled);
114 if (enabled && task_wants_autogroup(p, tg))
115 return p->signal->autogroup->tg;
117 return tg;
120 static void
121 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
123 struct autogroup *prev;
124 struct task_struct *t;
125 unsigned long flags;
127 BUG_ON(!lock_task_sighand(p, &flags));
129 prev = p->signal->autogroup;
130 if (prev == ag) {
131 unlock_task_sighand(p, &flags);
132 return;
135 p->signal->autogroup = autogroup_kref_get(ag);
137 t = p;
138 do {
139 sched_move_task(t);
140 } while_each_thread(p, t);
142 unlock_task_sighand(p, &flags);
143 autogroup_kref_put(prev);
146 /* Allocates GFP_KERNEL, cannot be called under any spinlock */
147 void sched_autogroup_create_attach(struct task_struct *p)
149 struct autogroup *ag = autogroup_create();
151 autogroup_move_group(p, ag);
152 /* drop extra refrence added by autogroup_create() */
153 autogroup_kref_put(ag);
155 EXPORT_SYMBOL(sched_autogroup_create_attach);
157 /* Cannot be called under siglock. Currently has no users */
158 void sched_autogroup_detach(struct task_struct *p)
160 autogroup_move_group(p, &autogroup_default);
162 EXPORT_SYMBOL(sched_autogroup_detach);
164 void sched_autogroup_fork(struct signal_struct *sig)
166 sig->autogroup = autogroup_task_get(current);
169 void sched_autogroup_exit(struct signal_struct *sig)
171 autogroup_kref_put(sig->autogroup);
174 static int __init setup_autogroup(char *str)
176 sysctl_sched_autogroup_enabled = 0;
178 return 1;
181 __setup("noautogroup", setup_autogroup);
183 #ifdef CONFIG_PROC_FS
185 int proc_sched_autogroup_set_nice(struct task_struct *p, int *nice)
187 static unsigned long next = INITIAL_JIFFIES;
188 struct autogroup *ag;
189 int err;
191 if (*nice < -20 || *nice > 19)
192 return -EINVAL;
194 err = security_task_setnice(current, *nice);
195 if (err)
196 return err;
198 if (*nice < 0 && !can_nice(current, *nice))
199 return -EPERM;
201 /* this is a heavy operation taking global locks.. */
202 if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
203 return -EAGAIN;
205 next = HZ / 10 + jiffies;
206 ag = autogroup_task_get(p);
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);
214 autogroup_kref_put(ag);
216 return err;
219 void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
221 struct autogroup *ag = autogroup_task_get(p);
223 down_read(&ag->lock);
224 seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
225 up_read(&ag->lock);
227 autogroup_kref_put(ag);
229 #endif /* CONFIG_PROC_FS */
231 #ifdef CONFIG_SCHED_DEBUG
232 static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
234 return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
236 #endif /* CONFIG_SCHED_DEBUG */
238 #endif /* CONFIG_SCHED_AUTOGROUP */