sched: fix !SYSFS build breakage
[safe/jmp/linux-2.6] / kernel / user.c
1 /*
2  * The "user cache".
3  *
4  * (C) Copyright 1991-2000 Linus Torvalds
5  *
6  * We have a per-user structure to keep track of how many
7  * processes, files etc the user has claimed, in order to be
8  * able to have per-user limits for system resources. 
9  */
10
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/bitops.h>
15 #include <linux/key.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/user_namespace.h>
19
20 /*
21  * UID task count cache, to get fast user lookup in "alloc_uid"
22  * when changing user ID's (ie setuid() and friends).
23  */
24
25 #define UIDHASH_MASK            (UIDHASH_SZ - 1)
26 #define __uidhashfn(uid)        (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
27 #define uidhashentry(ns, uid)   ((ns)->uidhash_table + __uidhashfn((uid)))
28
29 static struct kmem_cache *uid_cachep;
30
31 /*
32  * The uidhash_lock is mostly taken from process context, but it is
33  * occasionally also taken from softirq/tasklet context, when
34  * task-structs get RCU-freed. Hence all locking must be softirq-safe.
35  * But free_uid() is also called with local interrupts disabled, and running
36  * local_bh_enable() with local interrupts disabled is an error - we'll run
37  * softirq callbacks, and they can unconditionally enable interrupts, and
38  * the caller of free_uid() didn't expect that..
39  */
40 static DEFINE_SPINLOCK(uidhash_lock);
41
42 struct user_struct root_user = {
43         .__count        = ATOMIC_INIT(1),
44         .processes      = ATOMIC_INIT(1),
45         .files          = ATOMIC_INIT(0),
46         .sigpending     = ATOMIC_INIT(0),
47         .mq_bytes       = 0,
48         .locked_shm     = 0,
49 #ifdef CONFIG_KEYS
50         .uid_keyring    = &root_user_keyring,
51         .session_keyring = &root_session_keyring,
52 #endif
53 #ifdef CONFIG_FAIR_USER_SCHED
54         .tg             = &init_task_group,
55 #endif
56 };
57
58 /*
59  * These routines must be called with the uidhash spinlock held!
60  */
61 static inline void uid_hash_insert(struct user_struct *up,
62                                                 struct hlist_head *hashent)
63 {
64         hlist_add_head(&up->uidhash_node, hashent);
65 }
66
67 static inline void uid_hash_remove(struct user_struct *up)
68 {
69         hlist_del_init(&up->uidhash_node);
70 }
71
72 static inline struct user_struct *uid_hash_find(uid_t uid,
73                                                 struct hlist_head *hashent)
74 {
75         struct user_struct *user;
76         struct hlist_node *h;
77
78         hlist_for_each_entry(user, h, hashent, uidhash_node) {
79                 if (user->uid == uid) {
80                         atomic_inc(&user->__count);
81                         return user;
82                 }
83         }
84
85         return NULL;
86 }
87
88 #ifdef CONFIG_FAIR_USER_SCHED
89
90 static void sched_destroy_user(struct user_struct *up)
91 {
92         sched_destroy_group(up->tg);
93 }
94
95 static int sched_create_user(struct user_struct *up)
96 {
97         int rc = 0;
98
99         up->tg = sched_create_group();
100         if (IS_ERR(up->tg))
101                 rc = -ENOMEM;
102
103         return rc;
104 }
105
106 static void sched_switch_user(struct task_struct *p)
107 {
108         sched_move_task(p);
109 }
110
111 #else   /* CONFIG_FAIR_USER_SCHED */
112
113 static void sched_destroy_user(struct user_struct *up) { }
114 static int sched_create_user(struct user_struct *up) { return 0; }
115 static void sched_switch_user(struct task_struct *p) { }
116
117 #endif  /* CONFIG_FAIR_USER_SCHED */
118
119 #if defined(CONFIG_FAIR_USER_SCHED) && defined(CONFIG_SYSFS)
120
121 static struct kobject uids_kobject; /* represents /sys/kernel/uids directory */
122 static DEFINE_MUTEX(uids_mutex);
123
124 static inline void uids_mutex_lock(void)
125 {
126         mutex_lock(&uids_mutex);
127 }
128
129 static inline void uids_mutex_unlock(void)
130 {
131         mutex_unlock(&uids_mutex);
132 }
133
134 /* return cpu shares held by the user */
135 ssize_t cpu_shares_show(struct kset *kset, char *buffer)
136 {
137         struct user_struct *up = container_of(kset, struct user_struct, kset);
138
139         return sprintf(buffer, "%lu\n", sched_group_shares(up->tg));
140 }
141
142 /* modify cpu shares held by the user */
143 ssize_t cpu_shares_store(struct kset *kset, const char *buffer, size_t size)
144 {
145         struct user_struct *up = container_of(kset, struct user_struct, kset);
146         unsigned long shares;
147         int rc;
148
149         sscanf(buffer, "%lu", &shares);
150
151         rc = sched_group_set_shares(up->tg, shares);
152
153         return (rc ? rc : size);
154 }
155
156 static void user_attr_init(struct subsys_attribute *sa, char *name, int mode)
157 {
158         sa->attr.name = name;
159         sa->attr.mode = mode;
160         sa->show = cpu_shares_show;
161         sa->store = cpu_shares_store;
162 }
163
164 /* Create "/sys/kernel/uids/<uid>" directory and
165  *  "/sys/kernel/uids/<uid>/cpu_share" file for this user.
166  */
167 static int user_kobject_create(struct user_struct *up)
168 {
169         struct kset *kset = &up->kset;
170         struct kobject *kobj = &kset->kobj;
171         int error;
172
173         memset(kset, 0, sizeof(struct kset));
174         kobj->parent = &uids_kobject;   /* create under /sys/kernel/uids dir */
175         kobject_set_name(kobj, "%d", up->uid);
176         kset_init(kset);
177         user_attr_init(&up->user_attr, "cpu_share", 0644);
178
179         error = kobject_add(kobj);
180         if (error)
181                 goto done;
182
183         error = sysfs_create_file(kobj, &up->user_attr.attr);
184         if (error)
185                 kobject_del(kobj);
186
187         kobject_uevent(kobj, KOBJ_ADD);
188
189 done:
190         return error;
191 }
192
193 /* create these in sysfs filesystem:
194  *      "/sys/kernel/uids" directory
195  *      "/sys/kernel/uids/0" directory (for root user)
196  *      "/sys/kernel/uids/0/cpu_share" file (for root user)
197  */
198 int __init uids_kobject_init(void)
199 {
200         int error;
201
202         /* create under /sys/kernel dir */
203         uids_kobject.parent = &kernel_subsys.kobj;
204         uids_kobject.kset = &kernel_subsys;
205         kobject_set_name(&uids_kobject, "uids");
206         kobject_init(&uids_kobject);
207
208         error = kobject_add(&uids_kobject);
209         if (!error)
210                 error = user_kobject_create(&root_user);
211
212         return error;
213 }
214
215 /* work function to remove sysfs directory for a user and free up
216  * corresponding structures.
217  */
218 static void remove_user_sysfs_dir(struct work_struct *w)
219 {
220         struct user_struct *up = container_of(w, struct user_struct, work);
221         struct kobject *kobj = &up->kset.kobj;
222         unsigned long flags;
223         int remove_user = 0;
224
225         /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
226          * atomic.
227          */
228         uids_mutex_lock();
229
230         local_irq_save(flags);
231
232         if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
233                 uid_hash_remove(up);
234                 remove_user = 1;
235                 spin_unlock_irqrestore(&uidhash_lock, flags);
236         } else {
237                 local_irq_restore(flags);
238         }
239
240         if (!remove_user)
241                 goto done;
242
243         sysfs_remove_file(kobj, &up->user_attr.attr);
244         kobject_uevent(kobj, KOBJ_REMOVE);
245         kobject_del(kobj);
246
247         sched_destroy_user(up);
248         key_put(up->uid_keyring);
249         key_put(up->session_keyring);
250         kmem_cache_free(uid_cachep, up);
251
252 done:
253         uids_mutex_unlock();
254 }
255
256 /* IRQs are disabled and uidhash_lock is held upon function entry.
257  * IRQ state (as stored in flags) is restored and uidhash_lock released
258  * upon function exit.
259  */
260 static inline void free_user(struct user_struct *up, unsigned long flags)
261 {
262         /* restore back the count */
263         atomic_inc(&up->__count);
264         spin_unlock_irqrestore(&uidhash_lock, flags);
265
266         INIT_WORK(&up->work, remove_user_sysfs_dir);
267         schedule_work(&up->work);
268 }
269
270 #else   /* CONFIG_FAIR_USER_SCHED && CONFIG_SYSFS */
271
272 static inline int user_kobject_create(struct user_struct *up) { return 0; }
273 static inline void uids_mutex_lock(void) { }
274 static inline void uids_mutex_unlock(void) { }
275
276 /* IRQs are disabled and uidhash_lock is held upon function entry.
277  * IRQ state (as stored in flags) is restored and uidhash_lock released
278  * upon function exit.
279  */
280 static inline void free_user(struct user_struct *up, unsigned long flags)
281 {
282         uid_hash_remove(up);
283         spin_unlock_irqrestore(&uidhash_lock, flags);
284         sched_destroy_user(up);
285         key_put(up->uid_keyring);
286         key_put(up->session_keyring);
287         kmem_cache_free(uid_cachep, up);
288 }
289
290 #endif
291
292 /*
293  * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
294  * caller must undo that ref with free_uid().
295  *
296  * If the user_struct could not be found, return NULL.
297  */
298 struct user_struct *find_user(uid_t uid)
299 {
300         struct user_struct *ret;
301         unsigned long flags;
302         struct user_namespace *ns = current->nsproxy->user_ns;
303
304         spin_lock_irqsave(&uidhash_lock, flags);
305         ret = uid_hash_find(uid, uidhashentry(ns, uid));
306         spin_unlock_irqrestore(&uidhash_lock, flags);
307         return ret;
308 }
309
310 void free_uid(struct user_struct *up)
311 {
312         unsigned long flags;
313
314         if (!up)
315                 return;
316
317         local_irq_save(flags);
318         if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
319                 free_user(up, flags);
320         else
321                 local_irq_restore(flags);
322 }
323
324 struct user_struct * alloc_uid(struct user_namespace *ns, uid_t uid)
325 {
326         struct hlist_head *hashent = uidhashentry(ns, uid);
327         struct user_struct *up;
328
329         /* Make uid_hash_find() + user_kobject_create() + uid_hash_insert()
330          * atomic.
331          */
332         uids_mutex_lock();
333
334         spin_lock_irq(&uidhash_lock);
335         up = uid_hash_find(uid, hashent);
336         spin_unlock_irq(&uidhash_lock);
337
338         if (!up) {
339                 struct user_struct *new;
340
341                 new = kmem_cache_alloc(uid_cachep, GFP_KERNEL);
342                 if (!new)
343                         return NULL;
344                 new->uid = uid;
345                 atomic_set(&new->__count, 1);
346                 atomic_set(&new->processes, 0);
347                 atomic_set(&new->files, 0);
348                 atomic_set(&new->sigpending, 0);
349 #ifdef CONFIG_INOTIFY_USER
350                 atomic_set(&new->inotify_watches, 0);
351                 atomic_set(&new->inotify_devs, 0);
352 #endif
353
354                 new->mq_bytes = 0;
355                 new->locked_shm = 0;
356
357                 if (alloc_uid_keyring(new, current) < 0) {
358                         kmem_cache_free(uid_cachep, new);
359                         return NULL;
360                 }
361
362                 if (sched_create_user(new) < 0) {
363                         key_put(new->uid_keyring);
364                         key_put(new->session_keyring);
365                         kmem_cache_free(uid_cachep, new);
366                         return NULL;
367                 }
368
369                 if (user_kobject_create(new)) {
370                         sched_destroy_user(new);
371                         key_put(new->uid_keyring);
372                         key_put(new->session_keyring);
373                         kmem_cache_free(uid_cachep, new);
374                         uids_mutex_unlock();
375                         return NULL;
376                 }
377
378                 /*
379                  * Before adding this, check whether we raced
380                  * on adding the same user already..
381                  */
382                 spin_lock_irq(&uidhash_lock);
383                 up = uid_hash_find(uid, hashent);
384                 if (up) {
385                         /* This case is not possible when CONFIG_FAIR_USER_SCHED
386                          * is defined, since we serialize alloc_uid() using
387                          * uids_mutex. Hence no need to call
388                          * sched_destroy_user() or remove_user_sysfs_dir().
389                          */
390                         key_put(new->uid_keyring);
391                         key_put(new->session_keyring);
392                         kmem_cache_free(uid_cachep, new);
393                 } else {
394                         uid_hash_insert(new, hashent);
395                         up = new;
396                 }
397                 spin_unlock_irq(&uidhash_lock);
398
399         }
400
401         uids_mutex_unlock();
402
403         return up;
404 }
405
406 void switch_uid(struct user_struct *new_user)
407 {
408         struct user_struct *old_user;
409
410         /* What if a process setreuid()'s and this brings the
411          * new uid over his NPROC rlimit?  We can check this now
412          * cheaply with the new uid cache, so if it matters
413          * we should be checking for it.  -DaveM
414          */
415         old_user = current->user;
416         atomic_inc(&new_user->processes);
417         atomic_dec(&old_user->processes);
418         switch_uid_keyring(new_user);
419         current->user = new_user;
420         sched_switch_user(current);
421
422         /*
423          * We need to synchronize with __sigqueue_alloc()
424          * doing a get_uid(p->user).. If that saw the old
425          * user value, we need to wait until it has exited
426          * its critical region before we can free the old
427          * structure.
428          */
429         smp_mb();
430         spin_unlock_wait(&current->sighand->siglock);
431
432         free_uid(old_user);
433         suid_keys(current);
434 }
435
436 void release_uids(struct user_namespace *ns)
437 {
438         int i;
439         unsigned long flags;
440         struct hlist_head *head;
441         struct hlist_node *nd;
442
443         spin_lock_irqsave(&uidhash_lock, flags);
444         /*
445          * collapse the chains so that the user_struct-s will
446          * be still alive, but not in hashes. subsequent free_uid()
447          * will free them.
448          */
449         for (i = 0; i < UIDHASH_SZ; i++) {
450                 head = ns->uidhash_table + i;
451                 while (!hlist_empty(head)) {
452                         nd = head->first;
453                         hlist_del_init(nd);
454                 }
455         }
456         spin_unlock_irqrestore(&uidhash_lock, flags);
457
458         free_uid(ns->root_user);
459 }
460
461 static int __init uid_cache_init(void)
462 {
463         int n;
464
465         uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
466                         0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
467
468         for(n = 0; n < UIDHASH_SZ; ++n)
469                 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
470
471         /* Insert the root user immediately (init already runs as root) */
472         spin_lock_irq(&uidhash_lock);
473         uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
474         spin_unlock_irq(&uidhash_lock);
475
476         return 0;
477 }
478
479 module_init(uid_cache_init);