power:add UTC time when entry and exit suspend
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / kernel / user.c
CommitLineData
1da177e4
LT
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>
8703e8a4 16#include <linux/sched/user.h>
4021cb27 17#include <linux/interrupt.h>
9984de1a 18#include <linux/export.h>
acce292c 19#include <linux/user_namespace.h>
2c718bec 20#include <linux/proc_fs.h>
0bb80f24 21#include <linux/proc_ns.h>
1da177e4 22
59607db3
SH
23/*
24 * userns count is 1 for root user, 1 for init_uts_ns,
25 * and 1 for... ?
26 */
aee16ce7 27struct user_namespace init_user_ns = {
22d917d8
EB
28 .uid_map = {
29 .nr_extents = 1,
30 .extent[0] = {
31 .first = 0,
32 .lower_first = 0,
4b06a81f 33 .count = 4294967295U,
22d917d8
EB
34 },
35 },
36 .gid_map = {
37 .nr_extents = 1,
38 .extent[0] = {
39 .first = 0,
40 .lower_first = 0,
4b06a81f 41 .count = 4294967295U,
22d917d8
EB
42 },
43 },
f76d207a
EB
44 .projid_map = {
45 .nr_extents = 1,
46 .extent[0] = {
47 .first = 0,
48 .lower_first = 0,
49 .count = 4294967295U,
50 },
51 },
c61a2810 52 .count = ATOMIC_INIT(3),
783291e6
EB
53 .owner = GLOBAL_ROOT_UID,
54 .group = GLOBAL_ROOT_GID,
435d5f4b 55 .ns.inum = PROC_USER_INIT_INO,
33c42940
AV
56#ifdef CONFIG_USER_NS
57 .ns.ops = &userns_operations,
58#endif
9cc46516 59 .flags = USERNS_INIT_FLAGS,
6bd364d8
XG
60#ifdef CONFIG_PERSISTENT_KEYRINGS
61 .persistent_keyring_register_sem =
62 __RWSEM_INITIALIZER(init_user_ns.persistent_keyring_register_sem),
f36f8c75 63#endif
aee16ce7
PE
64};
65EXPORT_SYMBOL_GPL(init_user_ns);
66
1da177e4
LT
67/*
68 * UID task count cache, to get fast user lookup in "alloc_uid"
69 * when changing user ID's (ie setuid() and friends).
70 */
71
7b44ab97
EB
72#define UIDHASH_BITS (CONFIG_BASE_SMALL ? 3 : 7)
73#define UIDHASH_SZ (1 << UIDHASH_BITS)
1da177e4
LT
74#define UIDHASH_MASK (UIDHASH_SZ - 1)
75#define __uidhashfn(uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK)
7b44ab97 76#define uidhashentry(uid) (uidhash_table + __uidhashfn((__kuid_val(uid))))
1da177e4 77
e18b890b 78static struct kmem_cache *uid_cachep;
7b44ab97 79struct hlist_head uidhash_table[UIDHASH_SZ];
4021cb27
IM
80
81/*
82 * The uidhash_lock is mostly taken from process context, but it is
83 * occasionally also taken from softirq/tasklet context, when
84 * task-structs get RCU-freed. Hence all locking must be softirq-safe.
3fa97c9d
AM
85 * But free_uid() is also called with local interrupts disabled, and running
86 * local_bh_enable() with local interrupts disabled is an error - we'll run
87 * softirq callbacks, and they can unconditionally enable interrupts, and
88 * the caller of free_uid() didn't expect that..
4021cb27 89 */
1da177e4
LT
90static DEFINE_SPINLOCK(uidhash_lock);
91
783291e6 92/* root_user.__count is 1, for init task cred */
1da177e4 93struct user_struct root_user = {
783291e6 94 .__count = ATOMIC_INIT(1),
1da177e4 95 .processes = ATOMIC_INIT(1),
1da177e4 96 .sigpending = ATOMIC_INIT(0),
1da177e4 97 .locked_shm = 0,
7b44ab97 98 .uid = GLOBAL_ROOT_UID,
1da177e4
LT
99};
100
5cb350ba
DG
101/*
102 * These routines must be called with the uidhash spinlock held!
103 */
40aeb400 104static void uid_hash_insert(struct user_struct *up, struct hlist_head *hashent)
5cb350ba
DG
105{
106 hlist_add_head(&up->uidhash_node, hashent);
107}
108
40aeb400 109static void uid_hash_remove(struct user_struct *up)
5cb350ba
DG
110{
111 hlist_del_init(&up->uidhash_node);
112}
113
7b44ab97 114static struct user_struct *uid_hash_find(kuid_t uid, struct hlist_head *hashent)
3959214f
KS
115{
116 struct user_struct *user;
3959214f 117
b67bfe0d 118 hlist_for_each_entry(user, hashent, uidhash_node) {
7b44ab97 119 if (uid_eq(user->uid, uid)) {
3959214f
KS
120 atomic_inc(&user->__count);
121 return user;
122 }
123 }
124
125 return NULL;
126}
127
5cb350ba
DG
128/* IRQs are disabled and uidhash_lock is held upon function entry.
129 * IRQ state (as stored in flags) is restored and uidhash_lock released
130 * upon function exit.
131 */
18b6e041 132static void free_user(struct user_struct *up, unsigned long flags)
571428be 133 __releases(&uidhash_lock)
5cb350ba
DG
134{
135 uid_hash_remove(up);
136 spin_unlock_irqrestore(&uidhash_lock, flags);
5cb350ba
DG
137 key_put(up->uid_keyring);
138 key_put(up->session_keyring);
139 kmem_cache_free(uid_cachep, up);
140}
141
1da177e4
LT
142/*
143 * Locate the user_struct for the passed UID. If found, take a ref on it. The
144 * caller must undo that ref with free_uid().
145 *
146 * If the user_struct could not be found, return NULL.
147 */
7b44ab97 148struct user_struct *find_user(kuid_t uid)
1da177e4
LT
149{
150 struct user_struct *ret;
3fa97c9d 151 unsigned long flags;
1da177e4 152
3fa97c9d 153 spin_lock_irqsave(&uidhash_lock, flags);
7b44ab97 154 ret = uid_hash_find(uid, uidhashentry(uid));
3fa97c9d 155 spin_unlock_irqrestore(&uidhash_lock, flags);
1da177e4
LT
156 return ret;
157}
158
159void free_uid(struct user_struct *up)
160{
3fa97c9d
AM
161 unsigned long flags;
162
36f57413
AM
163 if (!up)
164 return;
165
3fa97c9d 166 local_irq_save(flags);
5cb350ba
DG
167 if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
168 free_user(up, flags);
169 else
36f57413 170 local_irq_restore(flags);
1da177e4
LT
171}
172
7b44ab97 173struct user_struct *alloc_uid(kuid_t uid)
1da177e4 174{
7b44ab97 175 struct hlist_head *hashent = uidhashentry(uid);
8eb703e4 176 struct user_struct *up, *new;
1da177e4 177
3fa97c9d 178 spin_lock_irq(&uidhash_lock);
1da177e4 179 up = uid_hash_find(uid, hashent);
3fa97c9d 180 spin_unlock_irq(&uidhash_lock);
1da177e4
LT
181
182 if (!up) {
354a1f4d 183 new = kmem_cache_zalloc(uid_cachep, GFP_KERNEL);
8eb703e4
PE
184 if (!new)
185 goto out_unlock;
5e8869bb 186
1da177e4
LT
187 new->uid = uid;
188 atomic_set(&new->__count, 1);
1da177e4
LT
189
190 /*
191 * Before adding this, check whether we raced
192 * on adding the same user already..
193 */
3fa97c9d 194 spin_lock_irq(&uidhash_lock);
1da177e4
LT
195 up = uid_hash_find(uid, hashent);
196 if (up) {
197 key_put(new->uid_keyring);
198 key_put(new->session_keyring);
199 kmem_cache_free(uid_cachep, new);
200 } else {
201 uid_hash_insert(new, hashent);
202 up = new;
203 }
3fa97c9d 204 spin_unlock_irq(&uidhash_lock);
1da177e4 205 }
2c718bec 206 proc_register_uid(uid);
5cb350ba 207
1da177e4 208 return up;
8eb703e4 209
8eb703e4 210out_unlock:
8eb703e4 211 return NULL;
1da177e4
LT
212}
213
1da177e4
LT
214static int __init uid_cache_init(void)
215{
216 int n;
217
218 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
20c2df83 219 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4
LT
220
221 for(n = 0; n < UIDHASH_SZ; ++n)
7b44ab97 222 INIT_HLIST_HEAD(uidhash_table + n);
1da177e4
LT
223
224 /* Insert the root user immediately (init already runs as root) */
3fa97c9d 225 spin_lock_irq(&uidhash_lock);
7b44ab97 226 uid_hash_insert(&root_user, uidhashentry(GLOBAL_ROOT_UID));
3fa97c9d 227 spin_unlock_irq(&uidhash_lock);
2c718bec 228 proc_register_uid(GLOBAL_ROOT_UID);
1da177e4
LT
229
230 return 0;
231}
c96d6660 232subsys_initcall(uid_cache_init);