Thu Apr 1 06:25:46 2021 UTC ()
Add a sysctl hashstat collector for uihash.


(simonb)
diff -r1.11 -r1.12 src/sys/kern/kern_uidinfo.c

cvs diff -r1.11 -r1.12 src/sys/kern/kern_uidinfo.c (switch to unified diff)

--- src/sys/kern/kern_uidinfo.c 2019/03/01 03:03:19 1.11
+++ src/sys/kern/kern_uidinfo.c 2021/04/01 06:25:45 1.12
@@ -1,258 +1,290 @@ @@ -1,258 +1,290 @@
1/* $NetBSD: kern_uidinfo.c,v 1.11 2019/03/01 03:03:19 christos Exp $ */ 1/* $NetBSD: kern_uidinfo.c,v 1.12 2021/04/01 06:25:45 simonb Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1982, 1986, 1991, 1993 4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc. 6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed 7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph 8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc. 10 * the permission of UNIX System Laboratories, Inc.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
15 * 1. Redistributions of source code must retain the above copyright 15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer. 16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright 17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the 18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution. 19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors 20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software 21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission. 22 * without specific prior written permission.
23 * 23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 */ 35 */
36 36
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.11 2019/03/01 03:03:19 christos Exp $"); 38__KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.12 2021/04/01 06:25:45 simonb Exp $");
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/kmem.h> 42#include <sys/kmem.h>
43#include <sys/proc.h> 43#include <sys/proc.h>
44#include <sys/atomic.h> 44#include <sys/atomic.h>
45#include <sys/uidinfo.h> 45#include <sys/uidinfo.h>
46#include <sys/sysctl.h> 46#include <sys/sysctl.h>
47#include <sys/kauth.h> 47#include <sys/kauth.h>
48#include <sys/cpu.h> 48#include <sys/cpu.h>
49 49
50static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl; 50static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl;
51static u_long uihash; 51static u_long uihash;
52 52
53#define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 53#define UIHASH(uid) (&uihashtbl[(uid) & uihash])
54 54
55static int 55static int
56sysctl_kern_uidinfo_cnt(SYSCTLFN_ARGS) 56sysctl_kern_uidinfo_cnt(SYSCTLFN_ARGS)
57{ 57{
58 static const struct { 58 static const struct {
59 const char *name; 59 const char *name;
60 u_int value; 60 u_int value;
61 } nv[] = { 61 } nv[] = {
62#define _MEM(n) { # n, offsetof(struct uidinfo, ui_ ## n) } 62#define _MEM(n) { # n, offsetof(struct uidinfo, ui_ ## n) }
63 _MEM(proccnt), 63 _MEM(proccnt),
64 _MEM(lwpcnt), 64 _MEM(lwpcnt),
65 _MEM(lockcnt), 65 _MEM(lockcnt),
66 _MEM(semcnt), 66 _MEM(semcnt),
67 _MEM(sbsize), 67 _MEM(sbsize),
68#undef _MEM 68#undef _MEM
69 }; 69 };
70 70
71 for (size_t i = 0; i < __arraycount(nv); i++) 71 for (size_t i = 0; i < __arraycount(nv); i++)
72 if (strcmp(nv[i].name, rnode->sysctl_name) == 0) { 72 if (strcmp(nv[i].name, rnode->sysctl_name) == 0) {
73 uint64_t cnt; 73 uint64_t cnt;
74 struct sysctlnode node = *rnode; 74 struct sysctlnode node = *rnode;
75 struct uidinfo *uip; 75 struct uidinfo *uip;
76 76
77 node.sysctl_data = &cnt; 77 node.sysctl_data = &cnt;
78 uip = uid_find(kauth_cred_geteuid(l->l_cred)); 78 uip = uid_find(kauth_cred_geteuid(l->l_cred));
79 79
80 *(uint64_t *)node.sysctl_data = 80 *(uint64_t *)node.sysctl_data =
81 *(u_long *)((char *)uip + nv[i].value); 81 *(u_long *)((char *)uip + nv[i].value);
82 82
83 return sysctl_lookup(SYSCTLFN_CALL(&node)); 83 return sysctl_lookup(SYSCTLFN_CALL(&node));
84 } 84 }
85 85
86 return EINVAL; 86 return EINVAL;
87} 87}
88 88
89static struct sysctllog *kern_uidinfo_sysctllog; 89static struct sysctllog *kern_uidinfo_sysctllog;
90 90
91static void 91static void
92sysctl_kern_uidinfo_setup(void) 92sysctl_kern_uidinfo_setup(void)
93{ 93{
94 const struct sysctlnode *rnode, *cnode; 94 const struct sysctlnode *rnode, *cnode;
95 95
96 sysctl_createv(&kern_uidinfo_sysctllog, 0, NULL, &rnode, 96 sysctl_createv(&kern_uidinfo_sysctllog, 0, NULL, &rnode,
97 CTLFLAG_PERMANENT, 97 CTLFLAG_PERMANENT,
98 CTLTYPE_NODE, "uidinfo", 98 CTLTYPE_NODE, "uidinfo",
99 SYSCTL_DESCR("Resource usage per uid"), 99 SYSCTL_DESCR("Resource usage per uid"),
100 NULL, 0, NULL, 0, 100 NULL, 0, NULL, 0,
101 CTL_KERN, CTL_CREATE, CTL_EOL); 101 CTL_KERN, CTL_CREATE, CTL_EOL);
102 102
103 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 103 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
104 CTLFLAG_PERMANENT, 104 CTLFLAG_PERMANENT,
105 CTLTYPE_QUAD, "proccnt", 105 CTLTYPE_QUAD, "proccnt",
106 SYSCTL_DESCR("Number of processes for the current user"), 106 SYSCTL_DESCR("Number of processes for the current user"),
107 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 107 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
108 CTL_CREATE, CTL_EOL); 108 CTL_CREATE, CTL_EOL);
109 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 109 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
110 CTLFLAG_PERMANENT, 110 CTLFLAG_PERMANENT,
111 CTLTYPE_QUAD, "lwpcnt", 111 CTLTYPE_QUAD, "lwpcnt",
112 SYSCTL_DESCR("Number of lwps for the current user"), 112 SYSCTL_DESCR("Number of lwps for the current user"),
113 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 113 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
114 CTL_CREATE, CTL_EOL); 114 CTL_CREATE, CTL_EOL);
115 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 115 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
116 CTLFLAG_PERMANENT, 116 CTLFLAG_PERMANENT,
117 CTLTYPE_QUAD, "lockcnt", 117 CTLTYPE_QUAD, "lockcnt",
118 SYSCTL_DESCR("Number of locks for the current user"), 118 SYSCTL_DESCR("Number of locks for the current user"),
119 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 119 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
120 CTL_CREATE, CTL_EOL); 120 CTL_CREATE, CTL_EOL);
121 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 121 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
122 CTLFLAG_PERMANENT, 122 CTLFLAG_PERMANENT,
123 CTLTYPE_QUAD, "semcnt", 123 CTLTYPE_QUAD, "semcnt",
124 SYSCTL_DESCR("Number of semaphores used for the current user"), 124 SYSCTL_DESCR("Number of semaphores used for the current user"),
125 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 125 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
126 CTL_CREATE, CTL_EOL); 126 CTL_CREATE, CTL_EOL);
127 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 127 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
128 CTLFLAG_PERMANENT, 128 CTLFLAG_PERMANENT,
129 CTLTYPE_QUAD, "sbsize", 129 CTLTYPE_QUAD, "sbsize",
130 SYSCTL_DESCR("Socket buffers used for the current user"), 130 SYSCTL_DESCR("Socket buffers used for the current user"),
131 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 131 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
132 CTL_CREATE, CTL_EOL); 132 CTL_CREATE, CTL_EOL);
133} 133}
134 134
 135static int
 136uid_stats(struct hashstat_sysctl *hs, bool fill)
 137{
 138 struct uidinfo *uip;
 139 uint64_t chain;
 140
 141 strlcpy(hs->hash_name, "uihash", sizeof(hs->hash_name));
 142 strlcpy(hs->hash_desc, "user info (uid->used proc) hash",
 143 sizeof(hs->hash_desc));
 144 if (!fill)
 145 return 0;
 146
 147 hs->hash_size = uihash + 1;
 148
 149 for (size_t i = 0; i < hs->hash_size; i++) {
 150 chain = 0;
 151 SLIST_FOREACH(uip, &uihashtbl[i], ui_hash) {
 152 membar_datadep_consumer();
 153 chain++;
 154 }
 155 if (chain > 0) {
 156 hs->hash_used++;
 157 hs->hash_items += chain;
 158 if (chain > hs->hash_maxchain)
 159 hs->hash_maxchain = chain;
 160 }
 161 }
 162
 163 return 0;
 164}
 165
135void 166void
136uid_init(void) 167uid_init(void)
137{ 168{
138 169
139 /* 170 /*
140 * In case of MP system, SLIST_FOREACH would force a cache line 171 * In case of MP system, SLIST_FOREACH would force a cache line
141 * write-back for every modified 'uidinfo', thus we try to keep the 172 * write-back for every modified 'uidinfo', thus we try to keep the
142 * lists short. 173 * lists short.
143 */ 174 */
144 const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64); 175 const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64);
145 176
146 uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash); 177 uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash);
147 178
148 /* 179 /*
149 * Ensure that uid 0 is always in the user hash table, as 180 * Ensure that uid 0 is always in the user hash table, as
150 * sbreserve() expects it available from interrupt context. 181 * sbreserve() expects it available from interrupt context.
151 */ 182 */
152 (void)uid_find(0); 183 (void)uid_find(0);
153 sysctl_kern_uidinfo_setup(); 184 sysctl_kern_uidinfo_setup();
 185 hashstat_register("uihash", uid_stats);
154} 186}
155 187
156struct uidinfo * 188struct uidinfo *
157uid_find(uid_t uid) 189uid_find(uid_t uid)
158{ 190{
159 struct uidinfo *uip, *uip_first, *newuip; 191 struct uidinfo *uip, *uip_first, *newuip;
160 struct uihashhead *uipp; 192 struct uihashhead *uipp;
161 193
162 uipp = UIHASH(uid); 194 uipp = UIHASH(uid);
163 newuip = NULL; 195 newuip = NULL;
164 196
165 /* 197 /*
166 * To make insertion atomic, abstraction of SLIST will be violated. 198 * To make insertion atomic, abstraction of SLIST will be violated.
167 */ 199 */
168 uip_first = uipp->slh_first; 200 uip_first = uipp->slh_first;
169 again: 201 again:
170 SLIST_FOREACH(uip, uipp, ui_hash) { 202 SLIST_FOREACH(uip, uipp, ui_hash) {
171 membar_datadep_consumer(); 203 membar_datadep_consumer();
172 if (uip->ui_uid != uid) 204 if (uip->ui_uid != uid)
173 continue; 205 continue;
174 if (newuip != NULL) 206 if (newuip != NULL)
175 kmem_free(newuip, sizeof(*newuip)); 207 kmem_free(newuip, sizeof(*newuip));
176 return uip; 208 return uip;
177 } 209 }
178 if (newuip == NULL) 210 if (newuip == NULL)
179 newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP); 211 newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP);
180 newuip->ui_uid = uid; 212 newuip->ui_uid = uid;
181 213
182 /* 214 /*
183 * If atomic insert is unsuccessful, another thread might be 215 * If atomic insert is unsuccessful, another thread might be
184 * allocated this 'uid', thus full re-check is needed. 216 * allocated this 'uid', thus full re-check is needed.
185 */ 217 */
186 newuip->ui_hash.sle_next = uip_first; 218 newuip->ui_hash.sle_next = uip_first;
187 membar_producer(); 219 membar_producer();
188 uip = atomic_cas_ptr(&uipp->slh_first, uip_first, newuip); 220 uip = atomic_cas_ptr(&uipp->slh_first, uip_first, newuip);
189 if (uip != uip_first) { 221 if (uip != uip_first) {
190 uip_first = uip; 222 uip_first = uip;
191 goto again; 223 goto again;
192 } 224 }
193 225
194 return newuip; 226 return newuip;
195} 227}
196 228
197/* 229/*
198 * Change the count associated with number of processes 230 * Change the count associated with number of processes
199 * a given user is using. 231 * a given user is using.
200 */ 232 */
201int 233int
202chgproccnt(uid_t uid, int diff) 234chgproccnt(uid_t uid, int diff)
203{ 235{
204 struct uidinfo *uip; 236 struct uidinfo *uip;
205 long proccnt; 237 long proccnt;
206 238
207 uip = uid_find(uid); 239 uip = uid_find(uid);
208 proccnt = atomic_add_long_nv(&uip->ui_proccnt, diff); 240 proccnt = atomic_add_long_nv(&uip->ui_proccnt, diff);
209 KASSERT(proccnt >= 0); 241 KASSERT(proccnt >= 0);
210 return proccnt; 242 return proccnt;
211} 243}
212 244
213/* 245/*
214 * Change the count associated with number of lwps 246 * Change the count associated with number of lwps
215 * a given user is using. 247 * a given user is using.
216 */ 248 */
217int 249int
218chglwpcnt(uid_t uid, int diff) 250chglwpcnt(uid_t uid, int diff)
219{ 251{
220 struct uidinfo *uip; 252 struct uidinfo *uip;
221 long lwpcnt; 253 long lwpcnt;
222 254
223 uip = uid_find(uid); 255 uip = uid_find(uid);
224 lwpcnt = atomic_add_long_nv(&uip->ui_lwpcnt, diff); 256 lwpcnt = atomic_add_long_nv(&uip->ui_lwpcnt, diff);
225 KASSERT(lwpcnt >= 0); 257 KASSERT(lwpcnt >= 0);
226 return lwpcnt; 258 return lwpcnt;
227} 259}
228 260
229/* 261/*
230 * Change the count associated with number of semaphores 262 * Change the count associated with number of semaphores
231 * a given user is using. 263 * a given user is using.
232 */ 264 */
233int 265int
234chgsemcnt(uid_t uid, int diff) 266chgsemcnt(uid_t uid, int diff)
235{ 267{
236 struct uidinfo *uip; 268 struct uidinfo *uip;
237 long semcnt; 269 long semcnt;
238 270
239 uip = uid_find(uid); 271 uip = uid_find(uid);
240 semcnt = atomic_add_long_nv(&uip->ui_semcnt, diff); 272 semcnt = atomic_add_long_nv(&uip->ui_semcnt, diff);
241 KASSERT(semcnt >= 0); 273 KASSERT(semcnt >= 0);
242 return semcnt; 274 return semcnt;
243} 275}
244 276
245int 277int
246chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax) 278chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
247{ 279{
248 rlim_t nsb; 280 rlim_t nsb;
249 const long diff = to - *hiwat; 281 const long diff = to - *hiwat;
250 282
251 nsb = (rlim_t)atomic_add_long_nv((long *)&uip->ui_sbsize, diff); 283 nsb = (rlim_t)atomic_add_long_nv((long *)&uip->ui_sbsize, diff);
252 if (diff > 0 && nsb > xmax) { 284 if (diff > 0 && nsb > xmax) {
253 atomic_add_long((long *)&uip->ui_sbsize, -diff); 285 atomic_add_long((long *)&uip->ui_sbsize, -diff);
254 return 0; 286 return 0;
255 } 287 }
256 *hiwat = to; 288 *hiwat = to;
257 return 1; 289 return 1;
258} 290}