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