[PATCH] fix uidhash_lock <-> RCU deadlock
[firewire-audio.git] / kernel / user.c
blobd1ae2349347e1776f99f572bb355a0a8a6435443
1 /*
2 * The "user cache".
4 * (C) Copyright 1991-2000 Linus Torvalds
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 */
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>
19 * UID task count cache, to get fast user lookup in "alloc_uid"
20 * when changing user ID's (ie setuid() and friends).
23 #define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 8)
24 #define UIDHASH_SZ (1 << UIDHASH_BITS)
25 #define UIDHASH_MASK (UIDHASH_SZ - 1)
26 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
27 #define uidhashentry(uid) (uidhash_table + __uidhashfn((uid)))
29 static kmem_cache_t *uid_cachep;
30 static struct list_head uidhash_table[UIDHASH_SZ];
33 * The uidhash_lock is mostly taken from process context, but it is
34 * occasionally also taken from softirq/tasklet context, when
35 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
37 static DEFINE_SPINLOCK(uidhash_lock);
39 struct user_struct root_user = {
40 .__count = ATOMIC_INIT(1),
41 .processes = ATOMIC_INIT(1),
42 .files = ATOMIC_INIT(0),
43 .sigpending = ATOMIC_INIT(0),
44 .mq_bytes = 0,
45 .locked_shm = 0,
46 #ifdef CONFIG_KEYS
47 .uid_keyring = &root_user_keyring,
48 .session_keyring = &root_session_keyring,
49 #endif
53 * These routines must be called with the uidhash spinlock held!
55 static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
57 list_add(&up->uidhash_list, hashent);
60 static inline void uid_hash_remove(struct user_struct *up)
62 list_del(&up->uidhash_list);
65 static inline struct user_struct *uid_hash_find(uid_t uid, struct list_head *hashent)
67 struct list_head *up;
69 list_for_each(up, hashent) {
70 struct user_struct *user;
72 user = list_entry(up, struct user_struct, uidhash_list);
74 if(user->uid == uid) {
75 atomic_inc(&user->__count);
76 return user;
80 return NULL;
84 * Locate the user_struct for the passed UID. If found, take a ref on it. The
85 * caller must undo that ref with free_uid().
87 * If the user_struct could not be found, return NULL.
89 struct user_struct *find_user(uid_t uid)
91 struct user_struct *ret;
93 spin_lock_bh(&uidhash_lock);
94 ret = uid_hash_find(uid, uidhashentry(uid));
95 spin_unlock_bh(&uidhash_lock);
96 return ret;
99 void free_uid(struct user_struct *up)
101 local_bh_disable();
102 if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
103 uid_hash_remove(up);
104 key_put(up->uid_keyring);
105 key_put(up->session_keyring);
106 kmem_cache_free(uid_cachep, up);
107 spin_unlock(&uidhash_lock);
109 local_bh_enable();
112 struct user_struct * alloc_uid(uid_t uid)
114 struct list_head *hashent = uidhashentry(uid);
115 struct user_struct *up;
117 spin_lock_bh(&uidhash_lock);
118 up = uid_hash_find(uid, hashent);
119 spin_unlock_bh(&uidhash_lock);
121 if (!up) {
122 struct user_struct *new;
124 new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
125 if (!new)
126 return NULL;
127 new->uid = uid;
128 atomic_set(&new->__count, 1);
129 atomic_set(&new->processes, 0);
130 atomic_set(&new->files, 0);
131 atomic_set(&new->sigpending, 0);
132 #ifdef CONFIG_INOTIFY
133 atomic_set(&new->inotify_watches, 0);
134 atomic_set(&new->inotify_devs, 0);
135 #endif
137 new->mq_bytes = 0;
138 new->locked_shm = 0;
140 if (alloc_uid_keyring(new) < 0) {
141 kmem_cache_free(uid_cachep, new);
142 return NULL;
146 * Before adding this, check whether we raced
147 * on adding the same user already..
149 spin_lock_bh(&uidhash_lock);
150 up = uid_hash_find(uid, hashent);
151 if (up) {
152 key_put(new->uid_keyring);
153 key_put(new->session_keyring);
154 kmem_cache_free(uid_cachep, new);
155 } else {
156 uid_hash_insert(new, hashent);
157 up = new;
159 spin_unlock_bh(&uidhash_lock);
162 return up;
165 void switch_uid(struct user_struct *new_user)
167 struct user_struct *old_user;
169 /* What if a process setreuid()'s and this brings the
170 * new uid over his NPROC rlimit? We can check this now
171 * cheaply with the new uid cache, so if it matters
172 * we should be checking for it. -DaveM
174 old_user = current->user;
175 atomic_inc(&new_user->processes);
176 atomic_dec(&old_user->processes);
177 switch_uid_keyring(new_user);
178 current->user = new_user;
179 free_uid(old_user);
180 suid_keys(current);
184 static int __init uid_cache_init(void)
186 int n;
188 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
189 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
191 for(n = 0; n < UIDHASH_SZ; ++n)
192 INIT_LIST_HEAD(uidhash_table + n);
194 /* Insert the root user immediately (init already runs as root) */
195 spin_lock_bh(&uidhash_lock);
196 uid_hash_insert(&root_user, uidhashentry(0));
197 spin_unlock_bh(&uidhash_lock);
199 return 0;
202 module_init(uid_cache_init);