blob: d476307dd4b00109975b09837b62cace9bd2048d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ingo Molnar4021cb22006-01-25 15:23:07 +010016#include <linux/interrupt.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070017#include <linux/module.h>
18#include <linux/user_namespace.h>
David Howellsd84f4f92008-11-14 10:39:23 +110019#include "cred-internals.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Pavel Emelyanovaee16ce2008-02-08 04:18:23 -080021struct user_namespace init_user_ns = {
22 .kref = {
23 .refcount = ATOMIC_INIT(2),
24 },
25 .root_user = &root_user,
26};
27EXPORT_SYMBOL_GPL(init_user_ns);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * UID task count cache, to get fast user lookup in "alloc_uid"
31 * when changing user ID's (ie setuid() and friends).
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define UIDHASH_MASK (UIDHASH_SZ - 1)
35#define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
Cedric Le Goateracce2922007-07-15 23:40:59 -070036#define uidhashentry(ns, uid) ((ns)->uidhash_table + __uidhashfn((uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Christoph Lametere18b8902006-12-06 20:33:20 -080038static struct kmem_cache *uid_cachep;
Ingo Molnar4021cb22006-01-25 15:23:07 +010039
40/*
41 * The uidhash_lock is mostly taken from process context, but it is
42 * occasionally also taken from softirq/tasklet context, when
43 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
Andrew Morton3fa97c92006-01-31 16:34:26 -080044 * But free_uid() is also called with local interrupts disabled, and running
45 * local_bh_enable() with local interrupts disabled is an error - we'll run
46 * softirq callbacks, and they can unconditionally enable interrupts, and
47 * the caller of free_uid() didn't expect that..
Ingo Molnar4021cb22006-01-25 15:23:07 +010048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static DEFINE_SPINLOCK(uidhash_lock);
50
51struct user_struct root_user = {
52 .__count = ATOMIC_INIT(1),
53 .processes = ATOMIC_INIT(1),
54 .files = ATOMIC_INIT(0),
55 .sigpending = ATOMIC_INIT(0),
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .locked_shm = 0,
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010057#ifdef CONFIG_USER_SCHED
Ingo Molnar4cf86d72007-10-15 17:00:14 +020058 .tg = &init_task_group,
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Dhaval Giani5cb350b2007-10-15 17:00:14 +020062/*
63 * These routines must be called with the uidhash spinlock held!
64 */
Alexey Dobriyan40aeb402007-10-16 23:30:09 -070065static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
Dhaval Giani5cb350b2007-10-15 17:00:14 +020066{
67 hlist_add_head(&up->uidhash_node, hashent);
68}
69
Alexey Dobriyan40aeb402007-10-16 23:30:09 -070070static void uid_hash_remove(struct user_struct *up)
Dhaval Giani5cb350b2007-10-15 17:00:14 +020071{
72 hlist_del_init(&up->uidhash_node);
73}
74
Alexey Dobriyan40aeb402007-10-16 23:30:09 -070075static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
Dhaval Giani5cb350b2007-10-15 17:00:14 +020076{
77 struct user_struct *user;
78 struct hlist_node *h;
79
80 hlist_for_each_entry(user, h, hashent, uidhash_node) {
81 if (user->uid == uid) {
82 atomic_inc(&user->__count);
83 return user;
84 }
85 }
86
87 return NULL;
88}
89
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010090#ifdef CONFIG_USER_SCHED
Dhaval Giani5cb350b2007-10-15 17:00:14 +020091
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +020092static void sched_destroy_user(struct user_struct *up)
93{
94 sched_destroy_group(up->tg);
95}
96
97static int sched_create_user(struct user_struct *up)
98{
99 int rc = 0;
100
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200101 up->tg = sched_create_group(&root_task_group);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200102 if (IS_ERR(up->tg))
103 rc = -ENOMEM;
104
105 return rc;
106}
107
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100108#else /* CONFIG_USER_SCHED */
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200109
110static void sched_destroy_user(struct user_struct *up) { }
111static int sched_create_user(struct user_struct *up) { return 0; }
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200112
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100113#endif /* CONFIG_USER_SCHED */
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200114
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100115#if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS)
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200116
Kay Sieverseb41d942007-11-02 13:47:53 +0100117static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200118static DEFINE_MUTEX(uids_mutex);
119
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200120static inline void uids_mutex_lock(void)
121{
122 mutex_lock(&uids_mutex);
123}
124
125static inline void uids_mutex_unlock(void)
126{
127 mutex_unlock(&uids_mutex);
128}
129
Kay Sieverseb41d942007-11-02 13:47:53 +0100130/* uid directory attributes */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100131#ifdef CONFIG_FAIR_GROUP_SCHED
Kay Sieverseb41d942007-11-02 13:47:53 +0100132static ssize_t cpu_shares_show(struct kobject *kobj,
133 struct kobj_attribute *attr,
134 char *buf)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200135{
Kay Sieverseb41d942007-11-02 13:47:53 +0100136 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200137
Kay Sieverseb41d942007-11-02 13:47:53 +0100138 return sprintf(buf, "%lu\n", sched_group_shares(up->tg));
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200139}
140
Kay Sieverseb41d942007-11-02 13:47:53 +0100141static ssize_t cpu_shares_store(struct kobject *kobj,
142 struct kobj_attribute *attr,
143 const char *buf, size_t size)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200144{
Kay Sieverseb41d942007-11-02 13:47:53 +0100145 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200146 unsigned long shares;
147 int rc;
148
Kay Sieverseb41d942007-11-02 13:47:53 +0100149 sscanf(buf, "%lu", &shares);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200150
151 rc = sched_group_set_shares(up->tg, shares);
152
153 return (rc ? rc : size);
154}
155
Kay Sieverseb41d942007-11-02 13:47:53 +0100156static struct kobj_attribute cpu_share_attr =
157 __ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100158#endif
Kay Sieverseb41d942007-11-02 13:47:53 +0100159
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100160#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100161static ssize_t cpu_rt_runtime_show(struct kobject *kobj,
162 struct kobj_attribute *attr,
163 char *buf)
164{
165 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
166
Peter Zijlstraaf4491e2008-08-19 12:33:02 +0200167 return sprintf(buf, "%ld\n", sched_group_rt_runtime(up->tg));
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100168}
169
170static ssize_t cpu_rt_runtime_store(struct kobject *kobj,
171 struct kobj_attribute *attr,
172 const char *buf, size_t size)
173{
174 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
175 unsigned long rt_runtime;
176 int rc;
177
Peter Zijlstraaf4491e2008-08-19 12:33:02 +0200178 sscanf(buf, "%ld", &rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100179
180 rc = sched_group_set_rt_runtime(up->tg, rt_runtime);
181
182 return (rc ? rc : size);
183}
184
185static struct kobj_attribute cpu_rt_runtime_attr =
186 __ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200187
188static ssize_t cpu_rt_period_show(struct kobject *kobj,
189 struct kobj_attribute *attr,
190 char *buf)
191{
192 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
193
194 return sprintf(buf, "%lu\n", sched_group_rt_period(up->tg));
195}
196
197static ssize_t cpu_rt_period_store(struct kobject *kobj,
198 struct kobj_attribute *attr,
199 const char *buf, size_t size)
200{
201 struct user_struct *up = container_of(kobj, struct user_struct, kobj);
202 unsigned long rt_period;
203 int rc;
204
205 sscanf(buf, "%lu", &rt_period);
206
207 rc = sched_group_set_rt_period(up->tg, rt_period);
208
209 return (rc ? rc : size);
210}
211
212static struct kobj_attribute cpu_rt_period_attr =
213 __ATTR(cpu_rt_period, 0644, cpu_rt_period_show, cpu_rt_period_store);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100214#endif
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100215
Kay Sieverseb41d942007-11-02 13:47:53 +0100216/* default attributes per uid directory */
217static struct attribute *uids_attributes[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100218#ifdef CONFIG_FAIR_GROUP_SCHED
Kay Sieverseb41d942007-11-02 13:47:53 +0100219 &cpu_share_attr.attr,
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100220#endif
221#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100222 &cpu_rt_runtime_attr.attr,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200223 &cpu_rt_period_attr.attr,
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100224#endif
Kay Sieverseb41d942007-11-02 13:47:53 +0100225 NULL
226};
227
228/* the lifetime of user_struct is not managed by the core (now) */
229static void uids_release(struct kobject *kobj)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200230{
Kay Sieverseb41d942007-11-02 13:47:53 +0100231 return;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200232}
233
Kay Sieverseb41d942007-11-02 13:47:53 +0100234static struct kobj_type uids_ktype = {
235 .sysfs_ops = &kobj_sysfs_ops,
236 .default_attrs = uids_attributes,
237 .release = uids_release,
238};
239
240/* create /sys/kernel/uids/<uid>/cpu_share file for this user */
241static int uids_user_create(struct user_struct *up)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200242{
Kay Sieverseb41d942007-11-02 13:47:53 +0100243 struct kobject *kobj = &up->kobj;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200244 int error;
245
Kay Sieverseb41d942007-11-02 13:47:53 +0100246 memset(kobj, 0, sizeof(struct kobject));
Kay Sieverseb41d942007-11-02 13:47:53 +0100247 kobj->kset = uids_kset;
Greg Kroah-Hartmancf151262007-12-17 23:05:35 -0700248 error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
249 if (error) {
250 kobject_put(kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200251 goto done;
Greg Kroah-Hartmancf151262007-12-17 23:05:35 -0700252 }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200253
Srivatsa Vaddagirifb7dde32007-10-15 17:00:18 +0200254 kobject_uevent(kobj, KOBJ_ADD);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200255done:
256 return error;
257}
258
Kay Sieverseb41d942007-11-02 13:47:53 +0100259/* create these entries in sysfs:
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200260 * "/sys/kernel/uids" directory
261 * "/sys/kernel/uids/0" directory (for root user)
262 * "/sys/kernel/uids/0/cpu_share" file (for root user)
263 */
Kay Sieverseb41d942007-11-02 13:47:53 +0100264int __init uids_sysfs_init(void)
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200265{
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800266 uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
Kay Sieverseb41d942007-11-02 13:47:53 +0100267 if (!uids_kset)
268 return -ENOMEM;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200269
Kay Sieverseb41d942007-11-02 13:47:53 +0100270 return uids_user_create(&root_user);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200271}
272
273/* work function to remove sysfs directory for a user and free up
274 * corresponding structures.
275 */
276static void remove_user_sysfs_dir(struct work_struct *w)
277{
278 struct user_struct *up = container_of(w, struct user_struct, work);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200279 unsigned long flags;
280 int remove_user = 0;
281
282 /* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
283 * atomic.
284 */
285 uids_mutex_lock();
286
287 local_irq_save(flags);
288
289 if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
290 uid_hash_remove(up);
291 remove_user = 1;
292 spin_unlock_irqrestore(&uidhash_lock, flags);
293 } else {
294 local_irq_restore(flags);
295 }
296
297 if (!remove_user)
298 goto done;
299
Kay Sieverseb41d942007-11-02 13:47:53 +0100300 kobject_uevent(&up->kobj, KOBJ_REMOVE);
301 kobject_del(&up->kobj);
302 kobject_put(&up->kobj);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200303
304 sched_destroy_user(up);
305 key_put(up->uid_keyring);
306 key_put(up->session_keyring);
307 kmem_cache_free(uid_cachep, up);
308
309done:
310 uids_mutex_unlock();
311}
312
313/* IRQs are disabled and uidhash_lock is held upon function entry.
314 * IRQ state (as stored in flags) is restored and uidhash_lock released
315 * upon function exit.
316 */
317static inline void free_user(struct user_struct *up, unsigned long flags)
318{
319 /* restore back the count */
320 atomic_inc(&up->__count);
321 spin_unlock_irqrestore(&uidhash_lock, flags);
322
323 INIT_WORK(&up->work, remove_user_sysfs_dir);
324 schedule_work(&up->work);
325}
326
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100327#else /* CONFIG_USER_SCHED && CONFIG_SYSFS */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200328
Kay Sieverseb41d942007-11-02 13:47:53 +0100329int uids_sysfs_init(void) { return 0; }
330static inline int uids_user_create(struct user_struct *up) { return 0; }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200331static inline void uids_mutex_lock(void) { }
332static inline void uids_mutex_unlock(void) { }
333
334/* IRQs are disabled and uidhash_lock is held upon function entry.
335 * IRQ state (as stored in flags) is restored and uidhash_lock released
336 * upon function exit.
337 */
338static inline void free_user(struct user_struct *up, unsigned long flags)
339{
340 uid_hash_remove(up);
341 spin_unlock_irqrestore(&uidhash_lock, flags);
342 sched_destroy_user(up);
343 key_put(up->uid_keyring);
344 key_put(up->session_keyring);
345 kmem_cache_free(uid_cachep, up);
346}
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200347
Dhaval Gianib1a8c172007-10-17 16:55:11 +0200348#endif
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * Locate the user_struct for the passed UID. If found, take a ref on it. The
352 * caller must undo that ref with free_uid().
353 *
354 * If the user_struct could not be found, return NULL.
355 */
356struct user_struct *find_user(uid_t uid)
357{
358 struct user_struct *ret;
Andrew Morton3fa97c92006-01-31 16:34:26 -0800359 unsigned long flags;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700360 struct user_namespace *ns = current->nsproxy->user_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Andrew Morton3fa97c92006-01-31 16:34:26 -0800362 spin_lock_irqsave(&uidhash_lock, flags);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700363 ret = uid_hash_find(uid, uidhashentry(ns, uid));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800364 spin_unlock_irqrestore(&uidhash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ret;
366}
367
368void free_uid(struct user_struct *up)
369{
Andrew Morton3fa97c92006-01-31 16:34:26 -0800370 unsigned long flags;
371
Andrew Morton36f57412006-03-24 03:15:47 -0800372 if (!up)
373 return;
374
Andrew Morton3fa97c92006-01-31 16:34:26 -0800375 local_irq_save(flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200376 if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
377 free_user(up, flags);
378 else
Andrew Morton36f57412006-03-24 03:15:47 -0800379 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Andrew Morton354a1f42008-04-30 00:54:54 -0700382struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Pavel Emelyanov735de222007-09-18 22:46:44 -0700384 struct hlist_head *hashent = uidhashentry(ns, uid);
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100385 struct user_struct *up, *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Kay Sieverseb41d942007-11-02 13:47:53 +0100387 /* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200388 * atomic.
389 */
390 uids_mutex_lock();
391
Andrew Morton3fa97c92006-01-31 16:34:26 -0800392 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 up = uid_hash_find(uid, hashent);
Andrew Morton3fa97c92006-01-31 16:34:26 -0800394 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!up) {
Andrew Morton354a1f42008-04-30 00:54:54 -0700397 new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100398 if (!new)
399 goto out_unlock;
Pavel Emelyanov5e8869b2007-11-26 21:21:49 +0100400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 new->uid = uid;
402 atomic_set(&new->__count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100404 if (sched_create_user(new) < 0)
David Howells69664cf2008-04-29 01:01:31 -0700405 goto out_free_user;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200406
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100407 if (uids_user_create(new))
408 goto out_destoy_sched;
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Before adding this, check whether we raced
412 * on adding the same user already..
413 */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800414 spin_lock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 up = uid_hash_find(uid, hashent);
416 if (up) {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100417 /* This case is not possible when CONFIG_USER_SCHED
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200418 * is defined, since we serialize alloc_uid() using
419 * uids_mutex. Hence no need to call
420 * sched_destroy_user() or remove_user_sysfs_dir().
421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 key_put(new->uid_keyring);
423 key_put(new->session_keyring);
424 kmem_cache_free(uid_cachep, new);
425 } else {
426 uid_hash_insert(new, hashent);
427 up = new;
428 }
Andrew Morton3fa97c92006-01-31 16:34:26 -0800429 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 }
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200432
433 uids_mutex_unlock();
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return up;
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100436
437out_destoy_sched:
438 sched_destroy_user(new);
Pavel Emelyanov8eb703e2008-01-25 21:08:26 +0100439out_free_user:
440 kmem_cache_free(uid_cachep, new);
441out_unlock:
442 uids_mutex_unlock();
443 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Pavel Emelyanovaee16ce2008-02-08 04:18:23 -0800446#ifdef CONFIG_USER_NS
Pavel Emelyanov28f300d2007-09-18 22:46:45 -0700447void release_uids(struct user_namespace *ns)
448{
449 int i;
450 unsigned long flags;
451 struct hlist_head *head;
452 struct hlist_node *nd;
453
454 spin_lock_irqsave(&uidhash_lock, flags);
455 /*
456 * collapse the chains so that the user_struct-s will
457 * be still alive, but not in hashes. subsequent free_uid()
458 * will free them.
459 */
460 for (i = 0; i < UIDHASH_SZ; i++) {
461 head = ns->uidhash_table + i;
462 while (!hlist_empty(head)) {
463 nd = head->first;
464 hlist_del_init(nd);
465 }
466 }
467 spin_unlock_irqrestore(&uidhash_lock, flags);
468
469 free_uid(ns->root_user);
470}
Pavel Emelyanovaee16ce2008-02-08 04:18:23 -0800471#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473static int __init uid_cache_init(void)
474{
475 int n;
476
477 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +0900478 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 for(n = 0; n < UIDHASH_SZ; ++n)
Pavel Emelyanov735de222007-09-18 22:46:44 -0700481 INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Insert the root user immediately (init already runs as root) */
Andrew Morton3fa97c92006-01-31 16:34:26 -0800484 spin_lock_irq(&uidhash_lock);
Cedric Le Goateracce2922007-07-15 23:40:59 -0700485 uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
Andrew Morton3fa97c92006-01-31 16:34:26 -0800486 spin_unlock_irq(&uidhash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 return 0;
489}
490
491module_init(uid_cache_init);