Blame view

kernel/user.c 12.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * The "user cache".
   *
   * (C) Copyright 1991-2000 Linus Torvalds
   *
   * We have a per-user structure to keep track of how many
   * processes, files etc the user has claimed, in order to be
   * able to have per-user limits for system resources. 
   */
  
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/bitops.h>
  #include <linux/key.h>
4021cb279   Ingo Molnar   [PATCH] fix uidha...
16
  #include <linux/interrupt.h>
acce292c8   Cedric Le Goater   user namespace: a...
17
18
  #include <linux/module.h>
  #include <linux/user_namespace.h>
d84f4f992   David Howells   CRED: Inaugurate ...
19
  #include "cred-internals.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20

aee16ce73   Pavel Emelyanov   namespaces: clean...
21
22
  struct user_namespace init_user_ns = {
  	.kref = {
1d1e97562   Serge E. Hallyn   keys: distinguish...
23
  		.refcount	= ATOMIC_INIT(2),
aee16ce73   Pavel Emelyanov   namespaces: clean...
24
  	},
18b6e0414   Serge Hallyn   User namespaces: ...
25
  	.creator = &root_user,
aee16ce73   Pavel Emelyanov   namespaces: clean...
26
27
  };
  EXPORT_SYMBOL_GPL(init_user_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
  /*
   * UID task count cache, to get fast user lookup in "alloc_uid"
   * when changing user ID's (ie setuid() and friends).
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
  #define UIDHASH_MASK		(UIDHASH_SZ - 1)
  #define __uidhashfn(uid)	(((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
acce292c8   Cedric Le Goater   user namespace: a...
34
  #define uidhashentry(ns, uid)	((ns)->uidhash_table + __uidhashfn((uid)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35

e18b890bb   Christoph Lameter   [PATCH] slab: rem...
36
  static struct kmem_cache *uid_cachep;
4021cb279   Ingo Molnar   [PATCH] fix uidha...
37
38
39
40
41
  
  /*
   * The uidhash_lock is mostly taken from process context, but it is
   * occasionally also taken from softirq/tasklet context, when
   * task-structs get RCU-freed. Hence all locking must be softirq-safe.
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
42
43
44
45
   * But free_uid() is also called with local interrupts disabled, and running
   * local_bh_enable() with local interrupts disabled is an error - we'll run
   * softirq callbacks, and they can unconditionally enable interrupts, and
   * the caller of free_uid() didn't expect that..
4021cb279   Ingo Molnar   [PATCH] fix uidha...
46
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  static DEFINE_SPINLOCK(uidhash_lock);
18b6e0414   Serge Hallyn   User namespaces: ...
48
  /* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->creator */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  struct user_struct root_user = {
18b6e0414   Serge Hallyn   User namespaces: ...
50
  	.__count	= ATOMIC_INIT(2),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
  	.processes	= ATOMIC_INIT(1),
  	.files		= ATOMIC_INIT(0),
  	.sigpending	= ATOMIC_INIT(0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  	.locked_shm     = 0,
18b6e0414   Serge Hallyn   User namespaces: ...
55
  	.user_ns	= &init_user_ns,
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
56
  #ifdef CONFIG_USER_SCHED
4cf86d77f   Ingo Molnar   sched: cleanup: r...
57
  	.tg		= &init_task_group,
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
58
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  };
5cb350baf   Dhaval Giani   sched: group sche...
60
61
62
  /*
   * These routines must be called with the uidhash spinlock held!
   */
40aeb400f   Alexey Dobriyan   user.c: deinline
63
  static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350baf   Dhaval Giani   sched: group sche...
64
65
66
  {
  	hlist_add_head(&up->uidhash_node, hashent);
  }
40aeb400f   Alexey Dobriyan   user.c: deinline
67
  static void uid_hash_remove(struct user_struct *up)
5cb350baf   Dhaval Giani   sched: group sche...
68
69
  {
  	hlist_del_init(&up->uidhash_node);
fb5ae64fd   Serge E. Hallyn   User namespaces: ...
70
  	put_user_ns(up->user_ns);
5cb350baf   Dhaval Giani   sched: group sche...
71
  }
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
72
  #ifdef CONFIG_USER_SCHED
5cb350baf   Dhaval Giani   sched: group sche...
73

24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
74
75
76
77
78
79
80
81
  static void sched_destroy_user(struct user_struct *up)
  {
  	sched_destroy_group(up->tg);
  }
  
  static int sched_create_user(struct user_struct *up)
  {
  	int rc = 0;
eff766a65   Peter Zijlstra   sched: fix the ta...
82
  	up->tg = sched_create_group(&root_task_group);
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
83
84
  	if (IS_ERR(up->tg))
  		rc = -ENOMEM;
6c415b923   Arun R Bharadwaj   sched: add uid in...
85
  	set_tg_uid(up);
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
86
87
  	return rc;
  }
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
88
  #else	/* CONFIG_USER_SCHED */
b1a8c172c   Dhaval Giani   sched: fix !SYSFS...
89
90
91
  
  static void sched_destroy_user(struct user_struct *up) { }
  static int sched_create_user(struct user_struct *up) { return 0; }
b1a8c172c   Dhaval Giani   sched: fix !SYSFS...
92

052f1dc7e   Peter Zijlstra   sched: rt-group: ...
93
  #endif	/* CONFIG_USER_SCHED */
b1a8c172c   Dhaval Giani   sched: fix !SYSFS...
94

052f1dc7e   Peter Zijlstra   sched: rt-group: ...
95
  #if defined(CONFIG_USER_SCHED) && defined(CONFIG_SYSFS)
b1a8c172c   Dhaval Giani   sched: fix !SYSFS...
96

3959214f9   Kay Sievers   sched: delayed cl...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  {
  	struct user_struct *user;
  	struct hlist_node *h;
  
  	hlist_for_each_entry(user, h, hashent, uidhash_node) {
  		if (user->uid == uid) {
  			/* possibly resurrect an "almost deleted" object */
  			if (atomic_inc_return(&user->__count) == 1)
  				cancel_delayed_work(&user->work);
  			return user;
  		}
  	}
  
  	return NULL;
  }
eb41d9465   Kay Sievers   fix struct user_i...
113
  static struct kset *uids_kset; /* represents the /sys/kernel/uids/ directory */
b1a8c172c   Dhaval Giani   sched: fix !SYSFS...
114
  static DEFINE_MUTEX(uids_mutex);
5cb350baf   Dhaval Giani   sched: group sche...
115
116
117
118
  static inline void uids_mutex_lock(void)
  {
  	mutex_lock(&uids_mutex);
  }
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
119

5cb350baf   Dhaval Giani   sched: group sche...
120
121
122
123
  static inline void uids_mutex_unlock(void)
  {
  	mutex_unlock(&uids_mutex);
  }
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
124

eb41d9465   Kay Sievers   fix struct user_i...
125
  /* uid directory attributes */
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
126
  #ifdef CONFIG_FAIR_GROUP_SCHED
eb41d9465   Kay Sievers   fix struct user_i...
127
128
129
  static ssize_t cpu_shares_show(struct kobject *kobj,
  			       struct kobj_attribute *attr,
  			       char *buf)
5cb350baf   Dhaval Giani   sched: group sche...
130
  {
eb41d9465   Kay Sievers   fix struct user_i...
131
  	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
132

eb41d9465   Kay Sievers   fix struct user_i...
133
134
  	return sprintf(buf, "%lu
  ", sched_group_shares(up->tg));
5cb350baf   Dhaval Giani   sched: group sche...
135
  }
eb41d9465   Kay Sievers   fix struct user_i...
136
137
138
  static ssize_t cpu_shares_store(struct kobject *kobj,
  				struct kobj_attribute *attr,
  				const char *buf, size_t size)
5cb350baf   Dhaval Giani   sched: group sche...
139
  {
eb41d9465   Kay Sievers   fix struct user_i...
140
  	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
5cb350baf   Dhaval Giani   sched: group sche...
141
142
  	unsigned long shares;
  	int rc;
eb41d9465   Kay Sievers   fix struct user_i...
143
  	sscanf(buf, "%lu", &shares);
5cb350baf   Dhaval Giani   sched: group sche...
144
145
146
147
148
  
  	rc = sched_group_set_shares(up->tg, shares);
  
  	return (rc ? rc : size);
  }
eb41d9465   Kay Sievers   fix struct user_i...
149
150
  static struct kobj_attribute cpu_share_attr =
  	__ATTR(cpu_share, 0644, cpu_shares_show, cpu_shares_store);
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
151
  #endif
eb41d9465   Kay Sievers   fix struct user_i...
152

052f1dc7e   Peter Zijlstra   sched: rt-group: ...
153
  #ifdef CONFIG_RT_GROUP_SCHED
9f0c1e560   Peter Zijlstra   sched: rt-group: ...
154
155
156
157
158
  static ssize_t cpu_rt_runtime_show(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   char *buf)
  {
  	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
af4491e51   Peter Zijlstra   sched: rt-bandwid...
159
160
  	return sprintf(buf, "%ld
  ", sched_group_rt_runtime(up->tg));
9f0c1e560   Peter Zijlstra   sched: rt-group: ...
161
162
163
164
165
166
167
168
169
  }
  
  static ssize_t cpu_rt_runtime_store(struct kobject *kobj,
  				    struct kobj_attribute *attr,
  				    const char *buf, size_t size)
  {
  	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  	unsigned long rt_runtime;
  	int rc;
af4491e51   Peter Zijlstra   sched: rt-bandwid...
170
  	sscanf(buf, "%ld", &rt_runtime);
9f0c1e560   Peter Zijlstra   sched: rt-group: ...
171
172
173
174
175
176
177
178
  
  	rc = sched_group_set_rt_runtime(up->tg, rt_runtime);
  
  	return (rc ? rc : size);
  }
  
  static struct kobj_attribute cpu_rt_runtime_attr =
  	__ATTR(cpu_rt_runtime, 0644, cpu_rt_runtime_show, cpu_rt_runtime_store);
d0b27fa77   Peter Zijlstra   sched: rt-group: ...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  
  static ssize_t cpu_rt_period_show(struct kobject *kobj,
  				   struct kobj_attribute *attr,
  				   char *buf)
  {
  	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  
  	return sprintf(buf, "%lu
  ", sched_group_rt_period(up->tg));
  }
  
  static ssize_t cpu_rt_period_store(struct kobject *kobj,
  				    struct kobj_attribute *attr,
  				    const char *buf, size_t size)
  {
  	struct user_struct *up = container_of(kobj, struct user_struct, kobj);
  	unsigned long rt_period;
  	int rc;
  
  	sscanf(buf, "%lu", &rt_period);
  
  	rc = sched_group_set_rt_period(up->tg, rt_period);
  
  	return (rc ? rc : size);
  }
  
  static struct kobj_attribute cpu_rt_period_attr =
  	__ATTR(cpu_rt_period, 0644, cpu_rt_period_show, cpu_rt_period_store);
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
207
  #endif
9f0c1e560   Peter Zijlstra   sched: rt-group: ...
208

eb41d9465   Kay Sievers   fix struct user_i...
209
210
  /* default attributes per uid directory */
  static struct attribute *uids_attributes[] = {
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
211
  #ifdef CONFIG_FAIR_GROUP_SCHED
eb41d9465   Kay Sievers   fix struct user_i...
212
  	&cpu_share_attr.attr,
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
213
214
  #endif
  #ifdef CONFIG_RT_GROUP_SCHED
9f0c1e560   Peter Zijlstra   sched: rt-group: ...
215
  	&cpu_rt_runtime_attr.attr,
d0b27fa77   Peter Zijlstra   sched: rt-group: ...
216
  	&cpu_rt_period_attr.attr,
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
217
  #endif
eb41d9465   Kay Sievers   fix struct user_i...
218
219
220
221
222
  	NULL
  };
  
  /* the lifetime of user_struct is not managed by the core (now) */
  static void uids_release(struct kobject *kobj)
5cb350baf   Dhaval Giani   sched: group sche...
223
  {
eb41d9465   Kay Sievers   fix struct user_i...
224
  	return;
5cb350baf   Dhaval Giani   sched: group sche...
225
  }
eb41d9465   Kay Sievers   fix struct user_i...
226
227
228
229
230
  static struct kobj_type uids_ktype = {
  	.sysfs_ops = &kobj_sysfs_ops,
  	.default_attrs = uids_attributes,
  	.release = uids_release,
  };
94d6a5f73   Serge E. Hallyn   user namespaces: ...
231
232
233
234
235
236
237
  /*
   * Create /sys/kernel/uids/<uid>/cpu_share file for this user
   * We do not create this file for users in a user namespace (until
   * sysfs tagging is implemented).
   *
   * See Documentation/scheduler/sched-design-CFS.txt for ramifications.
   */
eb41d9465   Kay Sievers   fix struct user_i...
238
  static int uids_user_create(struct user_struct *up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
  {
eb41d9465   Kay Sievers   fix struct user_i...
240
  	struct kobject *kobj = &up->kobj;
5cb350baf   Dhaval Giani   sched: group sche...
241
  	int error;
eb41d9465   Kay Sievers   fix struct user_i...
242
  	memset(kobj, 0, sizeof(struct kobject));
c37bbb0fd   Serge E. Hallyn   user namespaces: ...
243
244
  	if (up->user_ns != &init_user_ns)
  		return 0;
eb41d9465   Kay Sievers   fix struct user_i...
245
  	kobj->kset = uids_kset;
cf15126b3   Greg Kroah-Hartman   Kobject: convert ...
246
247
248
  	error = kobject_init_and_add(kobj, &uids_ktype, NULL, "%d", up->uid);
  	if (error) {
  		kobject_put(kobj);
5cb350baf   Dhaval Giani   sched: group sche...
249
  		goto done;
cf15126b3   Greg Kroah-Hartman   Kobject: convert ...
250
  	}
5cb350baf   Dhaval Giani   sched: group sche...
251

fb7dde37e   Srivatsa Vaddagiri   sched: generate u...
252
  	kobject_uevent(kobj, KOBJ_ADD);
5cb350baf   Dhaval Giani   sched: group sche...
253
254
  done:
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  }
eb41d9465   Kay Sievers   fix struct user_i...
256
  /* create these entries in sysfs:
5cb350baf   Dhaval Giani   sched: group sche...
257
258
259
260
   * 	"/sys/kernel/uids" directory
   * 	"/sys/kernel/uids/0" directory (for root user)
   * 	"/sys/kernel/uids/0/cpu_share" file (for root user)
   */
eb41d9465   Kay Sievers   fix struct user_i...
261
  int __init uids_sysfs_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  {
0ff21e466   Greg Kroah-Hartman   kobject: convert ...
263
  	uids_kset = kset_create_and_add("uids", NULL, kernel_kobj);
eb41d9465   Kay Sievers   fix struct user_i...
264
265
  	if (!uids_kset)
  		return -ENOMEM;
5cb350baf   Dhaval Giani   sched: group sche...
266

eb41d9465   Kay Sievers   fix struct user_i...
267
  	return uids_user_create(&root_user);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  }
3959214f9   Kay Sievers   sched: delayed cl...
269
  /* delayed work function to remove sysfs directory for a user and free up
5cb350baf   Dhaval Giani   sched: group sche...
270
271
   * corresponding structures.
   */
be50b8342   Dhaval Giani   kernel/user.c: fi...
272
  static void cleanup_user_struct(struct work_struct *w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  {
3959214f9   Kay Sievers   sched: delayed cl...
274
  	struct user_struct *up = container_of(w, struct user_struct, work.work);
5cb350baf   Dhaval Giani   sched: group sche...
275
276
  	unsigned long flags;
  	int remove_user = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277

5cb350baf   Dhaval Giani   sched: group sche...
278
279
280
281
  	/* Make uid_hash_remove() + sysfs_remove_file() + kobject_del()
  	 * atomic.
  	 */
  	uids_mutex_lock();
3959214f9   Kay Sievers   sched: delayed cl...
282
283
  	spin_lock_irqsave(&uidhash_lock, flags);
  	if (atomic_read(&up->__count) == 0) {
5cb350baf   Dhaval Giani   sched: group sche...
284
285
  		uid_hash_remove(up);
  		remove_user = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
  	}
3959214f9   Kay Sievers   sched: delayed cl...
287
  	spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288

5cb350baf   Dhaval Giani   sched: group sche...
289
290
  	if (!remove_user)
  		goto done;
be50b8342   Dhaval Giani   kernel/user.c: fi...
291
292
293
294
295
  	if (up->user_ns == &init_user_ns) {
  		kobject_uevent(&up->kobj, KOBJ_REMOVE);
  		kobject_del(&up->kobj);
  		kobject_put(&up->kobj);
  	}
5cb350baf   Dhaval Giani   sched: group sche...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  
  	sched_destroy_user(up);
  	key_put(up->uid_keyring);
  	key_put(up->session_keyring);
  	kmem_cache_free(uid_cachep, up);
  
  done:
  	uids_mutex_unlock();
  }
  
  /* IRQs are disabled and uidhash_lock is held upon function entry.
   * IRQ state (as stored in flags) is restored and uidhash_lock released
   * upon function exit.
   */
18b6e0414   Serge Hallyn   User namespaces: ...
310
  static void free_user(struct user_struct *up, unsigned long flags)
5cb350baf   Dhaval Giani   sched: group sche...
311
  {
3959214f9   Kay Sievers   sched: delayed cl...
312
313
  	INIT_DELAYED_WORK(&up->work, cleanup_user_struct);
  	schedule_delayed_work(&up->work, msecs_to_jiffies(1000));
b00bc0b23   Thomas Gleixner   uids: Prevent tea...
314
  	spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
  }
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
316
  #else	/* CONFIG_USER_SCHED && CONFIG_SYSFS */
5cb350baf   Dhaval Giani   sched: group sche...
317

3959214f9   Kay Sievers   sched: delayed cl...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  static struct user_struct *uid_hash_find(uid_t uid, struct hlist_head *hashent)
  {
  	struct user_struct *user;
  	struct hlist_node *h;
  
  	hlist_for_each_entry(user, h, hashent, uidhash_node) {
  		if (user->uid == uid) {
  			atomic_inc(&user->__count);
  			return user;
  		}
  	}
  
  	return NULL;
  }
eb41d9465   Kay Sievers   fix struct user_i...
332
333
  int uids_sysfs_init(void) { return 0; }
  static inline int uids_user_create(struct user_struct *up) { return 0; }
5cb350baf   Dhaval Giani   sched: group sche...
334
335
336
337
338
339
340
  static inline void uids_mutex_lock(void) { }
  static inline void uids_mutex_unlock(void) { }
  
  /* IRQs are disabled and uidhash_lock is held upon function entry.
   * IRQ state (as stored in flags) is restored and uidhash_lock released
   * upon function exit.
   */
18b6e0414   Serge Hallyn   User namespaces: ...
341
  static void free_user(struct user_struct *up, unsigned long flags)
5cb350baf   Dhaval Giani   sched: group sche...
342
343
344
345
346
347
348
349
  {
  	uid_hash_remove(up);
  	spin_unlock_irqrestore(&uidhash_lock, flags);
  	sched_destroy_user(up);
  	key_put(up->uid_keyring);
  	key_put(up->session_keyring);
  	kmem_cache_free(uid_cachep, up);
  }
b1a8c172c   Dhaval Giani   sched: fix !SYSFS...
350
  #endif
5cb350baf   Dhaval Giani   sched: group sche...
351

54e991242   Dhaval Giani   sched: don't allo...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
  #if defined(CONFIG_RT_GROUP_SCHED) && defined(CONFIG_USER_SCHED)
  /*
   * We need to check if a setuid can take place. This function should be called
   * before successfully completing the setuid.
   */
  int task_can_switch_user(struct user_struct *up, struct task_struct *tsk)
  {
  
  	return sched_rt_can_attach(up->tg, tsk);
  
  }
  #else
  int task_can_switch_user(struct user_struct *up, struct task_struct *tsk)
  {
  	return 1;
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
374
375
376
377
  /*
   * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
   * caller must undo that ref with free_uid().
   *
   * If the user_struct could not be found, return NULL.
   */
  struct user_struct *find_user(uid_t uid)
  {
  	struct user_struct *ret;
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
378
  	unsigned long flags;
6ded6ab9b   Serge Hallyn   User namespaces: ...
379
  	struct user_namespace *ns = current_user_ns();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380

3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
381
  	spin_lock_irqsave(&uidhash_lock, flags);
acce292c8   Cedric Le Goater   user namespace: a...
382
  	ret = uid_hash_find(uid, uidhashentry(ns, uid));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
383
  	spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
385
386
387
388
  	return ret;
  }
  
  void free_uid(struct user_struct *up)
  {
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
389
  	unsigned long flags;
36f574135   Andrew Morton   [PATCH] free_uid(...
390
391
  	if (!up)
  		return;
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
392
  	local_irq_save(flags);
5cb350baf   Dhaval Giani   sched: group sche...
393
394
395
  	if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  		free_user(up, flags);
  	else
36f574135   Andrew Morton   [PATCH] free_uid(...
396
  		local_irq_restore(flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
  }
354a1f4d9   Andrew Morton   alloc_uid: cleanup
398
  struct user_struct *alloc_uid(struct user_namespace *ns, uid_t uid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  {
735de2230   Pavel Emelyanov   Convert uid hash ...
400
  	struct hlist_head *hashent = uidhashentry(ns, uid);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
401
  	struct user_struct *up, *new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402

eb41d9465   Kay Sievers   fix struct user_i...
403
  	/* Make uid_hash_find() + uids_user_create() + uid_hash_insert()
5cb350baf   Dhaval Giani   sched: group sche...
404
405
406
  	 * atomic.
  	 */
  	uids_mutex_lock();
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
407
  	spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
  	up = uid_hash_find(uid, hashent);
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
409
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
411
  
  	if (!up) {
354a1f4d9   Andrew Morton   alloc_uid: cleanup
412
  		new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
413
414
  		if (!new)
  			goto out_unlock;
5e8869bb6   Pavel Emelyanov   sched: don't forg...
415

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
417
  		new->uid = uid;
  		atomic_set(&new->__count, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418

8eb703e4f   Pavel Emelyanov   uids: merge multi...
419
  		if (sched_create_user(new) < 0)
69664cf16   David Howells   keys: don't gener...
420
  			goto out_free_user;
24e377a83   Srivatsa Vaddagiri   sched: add fair-u...
421

18b6e0414   Serge Hallyn   User namespaces: ...
422
  		new->user_ns = get_user_ns(ns);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
423
424
  		if (uids_user_create(new))
  			goto out_destoy_sched;
5cb350baf   Dhaval Giani   sched: group sche...
425

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
427
428
429
  		/*
  		 * Before adding this, check whether we raced
  		 * on adding the same user already..
  		 */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
430
  		spin_lock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
432
  		up = uid_hash_find(uid, hashent);
  		if (up) {
052f1dc7e   Peter Zijlstra   sched: rt-group: ...
433
  			/* This case is not possible when CONFIG_USER_SCHED
5cb350baf   Dhaval Giani   sched: group sche...
434
435
436
437
  			 * is defined, since we serialize alloc_uid() using
  			 * uids_mutex. Hence no need to call
  			 * sched_destroy_user() or remove_user_sysfs_dir().
  			 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
440
441
442
443
444
  			key_put(new->uid_keyring);
  			key_put(new->session_keyring);
  			kmem_cache_free(uid_cachep, new);
  		} else {
  			uid_hash_insert(new, hashent);
  			up = new;
  		}
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
445
  		spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
  	}
5cb350baf   Dhaval Giani   sched: group sche...
447
448
  
  	uids_mutex_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  	return up;
8eb703e4f   Pavel Emelyanov   uids: merge multi...
450
451
452
  
  out_destoy_sched:
  	sched_destroy_user(new);
18b6e0414   Serge Hallyn   User namespaces: ...
453
  	put_user_ns(new->user_ns);
8eb703e4f   Pavel Emelyanov   uids: merge multi...
454
455
456
457
458
  out_free_user:
  	kmem_cache_free(uid_cachep, new);
  out_unlock:
  	uids_mutex_unlock();
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
461
462
463
464
  static int __init uid_cache_init(void)
  {
  	int n;
  
  	uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
20c2df83d   Paul Mundt   mm: Remove slab d...
465
  			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
  
  	for(n = 0; n < UIDHASH_SZ; ++n)
735de2230   Pavel Emelyanov   Convert uid hash ...
468
  		INIT_HLIST_HEAD(init_user_ns.uidhash_table + n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
470
  
  	/* Insert the root user immediately (init already runs as root) */
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
471
  	spin_lock_irq(&uidhash_lock);
acce292c8   Cedric Le Goater   user namespace: a...
472
  	uid_hash_insert(&root_user, uidhashentry(&init_user_ns, 0));
3fa97c9db   Andrew Morton   [PATCH] "Fix uidh...
473
  	spin_unlock_irq(&uidhash_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
477
478
  
  	return 0;
  }
  
  module_init(uid_cache_init);