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