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.
11 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
17 * UID task count cache, to get fast user lookup in "alloc_uid"
18 * when changing user ID's (ie setuid() and friends).
20 #define UIDHASH_BITS 8
21 #define UIDHASH_SZ (1 << UIDHASH_BITS)
22 #define UIDHASH_MASK (UIDHASH_SZ - 1)
23 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) ^ uid) & UIDHASH_MASK)
24 #define uidhashentry(uid) (uidhash_table + __uidhashfn(uid))
26 static kmem_cache_t
*uid_cachep
;
27 static struct user_struct
*uidhash_table
[UIDHASH_SZ
];
28 static spinlock_t uidhash_lock
= SPIN_LOCK_UNLOCKED
;
30 struct user_struct root_user
= {
31 __count
: ATOMIC_INIT(1),
32 processes
: ATOMIC_INIT(1),
37 * These routines must be called with the uidhash spinlock held!
39 static inline void uid_hash_insert(struct user_struct
*up
, struct user_struct
**hashent
)
41 struct user_struct
*next
= *hashent
;
45 next
->pprev
= &up
->next
;
50 static inline void uid_hash_remove(struct user_struct
*up
)
52 struct user_struct
*next
= up
->next
;
53 struct user_struct
**pprev
= up
->pprev
;
60 static inline struct user_struct
*uid_hash_find(uid_t uid
, struct user_struct
**hashent
)
62 struct user_struct
*next
;
66 struct user_struct
*up
= next
;
71 atomic_inc(&up
->__count
);
78 * For SMP, we need to re-test the user struct counter
79 * after having acquired the spinlock. This allows us to do
80 * the common case (not freeing anything) without having
84 #define uid_hash_free(up) (!atomic_read(&(up)->__count))
86 #define uid_hash_free(up) (1)
89 void free_uid(struct user_struct
*up
)
92 if (atomic_dec_and_test(&up
->__count
)) {
93 spin_lock(&uidhash_lock
);
94 if (uid_hash_free(up
)) {
96 kmem_cache_free(uid_cachep
, up
);
98 spin_unlock(&uidhash_lock
);
103 struct user_struct
* alloc_uid(uid_t uid
)
105 struct user_struct
**hashent
= uidhashentry(uid
);
106 struct user_struct
*up
;
108 spin_lock(&uidhash_lock
);
109 up
= uid_hash_find(uid
, hashent
);
110 spin_unlock(&uidhash_lock
);
113 struct user_struct
*new;
115 new = kmem_cache_alloc(uid_cachep
, SLAB_KERNEL
);
119 atomic_set(&new->__count
, 1);
120 atomic_set(&new->processes
, 0);
121 atomic_set(&new->files
, 0);
124 * Before adding this, check whether we raced
125 * on adding the same user already..
127 spin_lock(&uidhash_lock
);
128 up
= uid_hash_find(uid
, hashent
);
130 kmem_cache_free(uid_cachep
, new);
132 uid_hash_insert(new, hashent
);
135 spin_unlock(&uidhash_lock
);
142 static int __init
uid_cache_init(void)
144 uid_cachep
= kmem_cache_create("uid_cache", sizeof(struct user_struct
),
146 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
148 panic("Cannot create uid taskcount SLAB cache\n");
150 /* Insert the root user immediately - init already runs with this */
151 uid_hash_insert(&root_user
, uidhashentry(0));
155 module_init(uid_cache_init
);