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