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