kern_uidinfo.c revision 1.5.2.2 1 1.5.2.2 yamt /* $NetBSD: kern_uidinfo.c,v 1.5.2.2 2009/05/04 08:13:47 yamt Exp $ */
2 1.5.2.2 yamt
3 1.5.2.2 yamt /*-
4 1.5.2.2 yamt * Copyright (c) 1982, 1986, 1991, 1993
5 1.5.2.2 yamt * The Regents of the University of California. All rights reserved.
6 1.5.2.2 yamt * (c) UNIX System Laboratories, Inc.
7 1.5.2.2 yamt * All or some portions of this file are derived from material licensed
8 1.5.2.2 yamt * to the University of California by American Telephone and Telegraph
9 1.5.2.2 yamt * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.5.2.2 yamt * the permission of UNIX System Laboratories, Inc.
11 1.5.2.2 yamt *
12 1.5.2.2 yamt * Redistribution and use in source and binary forms, with or without
13 1.5.2.2 yamt * modification, are permitted provided that the following conditions
14 1.5.2.2 yamt * are met:
15 1.5.2.2 yamt * 1. Redistributions of source code must retain the above copyright
16 1.5.2.2 yamt * notice, this list of conditions and the following disclaimer.
17 1.5.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
18 1.5.2.2 yamt * notice, this list of conditions and the following disclaimer in the
19 1.5.2.2 yamt * documentation and/or other materials provided with the distribution.
20 1.5.2.2 yamt * 3. Neither the name of the University nor the names of its contributors
21 1.5.2.2 yamt * may be used to endorse or promote products derived from this software
22 1.5.2.2 yamt * without specific prior written permission.
23 1.5.2.2 yamt *
24 1.5.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.5.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.5.2.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.5.2.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.5.2.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.5.2.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.5.2.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.5.2.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.5.2.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.5.2.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.5.2.2 yamt * SUCH DAMAGE.
35 1.5.2.2 yamt */
36 1.5.2.2 yamt
37 1.5.2.2 yamt #include <sys/cdefs.h>
38 1.5.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.5.2.2 2009/05/04 08:13:47 yamt Exp $");
39 1.5.2.2 yamt
40 1.5.2.2 yamt #include <sys/param.h>
41 1.5.2.2 yamt #include <sys/systm.h>
42 1.5.2.2 yamt #include <sys/kmem.h>
43 1.5.2.2 yamt #include <sys/proc.h>
44 1.5.2.2 yamt #include <sys/atomic.h>
45 1.5.2.2 yamt #include <sys/uidinfo.h>
46 1.5.2.2 yamt #include <sys/cpu.h>
47 1.5.2.2 yamt
48 1.5.2.2 yamt static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl;
49 1.5.2.2 yamt static u_long uihash;
50 1.5.2.2 yamt
51 1.5.2.2 yamt #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
52 1.5.2.2 yamt
53 1.5.2.2 yamt void
54 1.5.2.2 yamt uid_init(void)
55 1.5.2.2 yamt {
56 1.5.2.2 yamt
57 1.5.2.2 yamt /*
58 1.5.2.2 yamt * In case of MP system, SLIST_FOREACH would force a cache line
59 1.5.2.2 yamt * write-back for every modified 'uidinfo', thus we try to keep the
60 1.5.2.2 yamt * lists short.
61 1.5.2.2 yamt */
62 1.5.2.2 yamt const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64);
63 1.5.2.2 yamt
64 1.5.2.2 yamt uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash);
65 1.5.2.2 yamt
66 1.5.2.2 yamt /*
67 1.5.2.2 yamt * Ensure that uid 0 is always in the user hash table, as
68 1.5.2.2 yamt * sbreserve() expects it available from interrupt context.
69 1.5.2.2 yamt */
70 1.5.2.2 yamt (void)uid_find(0);
71 1.5.2.2 yamt }
72 1.5.2.2 yamt
73 1.5.2.2 yamt struct uidinfo *
74 1.5.2.2 yamt uid_find(uid_t uid)
75 1.5.2.2 yamt {
76 1.5.2.2 yamt struct uidinfo *uip, *uip_first, *newuip;
77 1.5.2.2 yamt struct uihashhead *uipp;
78 1.5.2.2 yamt
79 1.5.2.2 yamt uipp = UIHASH(uid);
80 1.5.2.2 yamt newuip = NULL;
81 1.5.2.2 yamt
82 1.5.2.2 yamt /*
83 1.5.2.2 yamt * To make insertion atomic, abstraction of SLIST will be violated.
84 1.5.2.2 yamt */
85 1.5.2.2 yamt uip_first = uipp->slh_first;
86 1.5.2.2 yamt again:
87 1.5.2.2 yamt SLIST_FOREACH(uip, uipp, ui_hash) {
88 1.5.2.2 yamt if (uip->ui_uid != uid)
89 1.5.2.2 yamt continue;
90 1.5.2.2 yamt if (newuip != NULL)
91 1.5.2.2 yamt kmem_free(newuip, sizeof(*newuip));
92 1.5.2.2 yamt return uip;
93 1.5.2.2 yamt }
94 1.5.2.2 yamt if (newuip == NULL)
95 1.5.2.2 yamt newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP);
96 1.5.2.2 yamt newuip->ui_uid = uid;
97 1.5.2.2 yamt
98 1.5.2.2 yamt /*
99 1.5.2.2 yamt * If atomic insert is unsuccessful, another thread might be
100 1.5.2.2 yamt * allocated this 'uid', thus full re-check is needed.
101 1.5.2.2 yamt */
102 1.5.2.2 yamt newuip->ui_hash.sle_next = uip_first;
103 1.5.2.2 yamt membar_producer();
104 1.5.2.2 yamt uip = atomic_cas_ptr(&uipp->slh_first, uip_first, newuip);
105 1.5.2.2 yamt if (uip != uip_first) {
106 1.5.2.2 yamt uip_first = uip;
107 1.5.2.2 yamt goto again;
108 1.5.2.2 yamt }
109 1.5.2.2 yamt
110 1.5.2.2 yamt return newuip;
111 1.5.2.2 yamt }
112 1.5.2.2 yamt
113 1.5.2.2 yamt /*
114 1.5.2.2 yamt * Change the count associated with number of processes
115 1.5.2.2 yamt * a given user is using.
116 1.5.2.2 yamt */
117 1.5.2.2 yamt int
118 1.5.2.2 yamt chgproccnt(uid_t uid, int diff)
119 1.5.2.2 yamt {
120 1.5.2.2 yamt struct uidinfo *uip;
121 1.5.2.2 yamt long proccnt;
122 1.5.2.2 yamt
123 1.5.2.2 yamt uip = uid_find(uid);
124 1.5.2.2 yamt proccnt = atomic_add_long_nv(&uip->ui_proccnt, diff);
125 1.5.2.2 yamt KASSERT(proccnt >= 0);
126 1.5.2.2 yamt return proccnt;
127 1.5.2.2 yamt }
128 1.5.2.2 yamt
129 1.5.2.2 yamt int
130 1.5.2.2 yamt chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
131 1.5.2.2 yamt {
132 1.5.2.2 yamt rlim_t nsb;
133 1.5.2.2 yamt const long diff = to - *hiwat;
134 1.5.2.2 yamt
135 1.5.2.2 yamt nsb = (rlim_t)atomic_add_long_nv((long *)&uip->ui_sbsize, diff);
136 1.5.2.2 yamt if (diff > 0 && nsb > xmax) {
137 1.5.2.2 yamt atomic_add_long((long *)&uip->ui_sbsize, -diff);
138 1.5.2.2 yamt return 0;
139 1.5.2.2 yamt }
140 1.5.2.2 yamt *hiwat = to;
141 1.5.2.2 yamt return 1;
142 1.5.2.2 yamt }
143