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