Home | History | Annotate | Line # | Download | only in kern
kern_uidinfo.c revision 1.9
      1  1.9  riastrad /*	$NetBSD: kern_uidinfo.c,v 1.9 2017/03/18 05:49:17 riastradh 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.9  riastrad __KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.9 2017/03/18 05:49:17 riastradh 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.6  christos #include <sys/sysctl.h>
     47  1.6  christos #include <sys/kauth.h>
     48  1.5        ad #include <sys/cpu.h>
     49  1.1     pooka 
     50  1.1     pooka static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl;
     51  1.1     pooka static u_long 		uihash;
     52  1.1     pooka 
     53  1.1     pooka #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
     54  1.1     pooka 
     55  1.6  christos static int
     56  1.6  christos sysctl_kern_uidinfo_cnt(SYSCTLFN_ARGS)
     57  1.9  riastrad {
     58  1.6  christos 	static const struct {
     59  1.6  christos 		const char *name;
     60  1.6  christos 		u_int value;
     61  1.6  christos 	} nv[] = {
     62  1.6  christos #define _MEM(n) { # n, offsetof(struct uidinfo, ui_ ## n) }
     63  1.6  christos 		_MEM(proccnt),
     64  1.6  christos 		_MEM(lwpcnt),
     65  1.6  christos 		_MEM(lockcnt),
     66  1.6  christos 		_MEM(sbsize),
     67  1.6  christos #undef _MEM
     68  1.6  christos 	};
     69  1.6  christos 
     70  1.6  christos 	for (size_t i = 0; i < __arraycount(nv); i++)
     71  1.6  christos 		if (strcmp(nv[i].name, rnode->sysctl_name) == 0) {
     72  1.6  christos 			uint64_t cnt;
     73  1.6  christos 			struct sysctlnode node = *rnode;
     74  1.6  christos 			struct uidinfo *uip;
     75  1.6  christos 
     76  1.6  christos 			node.sysctl_data = &cnt;
     77  1.6  christos 			uip = uid_find(kauth_cred_geteuid(l->l_cred));
     78  1.6  christos 
     79  1.9  riastrad 			*(uint64_t *)node.sysctl_data =
     80  1.6  christos 			    *(u_long *)((char *)uip + nv[i].value);
     81  1.6  christos 
     82  1.6  christos 			return sysctl_lookup(SYSCTLFN_CALL(&node));
     83  1.6  christos 		}
     84  1.6  christos 
     85  1.6  christos 	return EINVAL;
     86  1.6  christos }
     87  1.6  christos 
     88  1.8     pooka static struct sysctllog *kern_uidinfo_sysctllog;
     89  1.8     pooka 
     90  1.6  christos static void
     91  1.6  christos sysctl_kern_uidinfo_setup(void)
     92  1.6  christos {
     93  1.6  christos 	const struct sysctlnode *rnode, *cnode;
     94  1.6  christos 
     95  1.6  christos 	sysctl_createv(&kern_uidinfo_sysctllog, 0, NULL, &rnode,
     96  1.6  christos 		       CTLFLAG_PERMANENT,
     97  1.6  christos 		       CTLTYPE_NODE, "uidinfo",
     98  1.6  christos 		       SYSCTL_DESCR("Resource usage per uid"),
     99  1.6  christos 		       NULL, 0, NULL, 0,
    100  1.6  christos 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    101  1.6  christos 
    102  1.6  christos 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    103  1.6  christos 		       CTLFLAG_PERMANENT,
    104  1.6  christos 		       CTLTYPE_QUAD, "proccnt",
    105  1.6  christos 		       SYSCTL_DESCR("Number of processes for the current user"),
    106  1.6  christos 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    107  1.6  christos 		       CTL_CREATE, CTL_EOL);
    108  1.6  christos 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    109  1.6  christos 		       CTLFLAG_PERMANENT,
    110  1.6  christos 		       CTLTYPE_QUAD, "lwpcnt",
    111  1.6  christos 		       SYSCTL_DESCR("Number of lwps for the current user"),
    112  1.6  christos 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    113  1.6  christos 		       CTL_CREATE, CTL_EOL);
    114  1.6  christos 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    115  1.6  christos 		       CTLFLAG_PERMANENT,
    116  1.6  christos 		       CTLTYPE_QUAD, "lockcnt",
    117  1.6  christos 		       SYSCTL_DESCR("Number of locks for the current user"),
    118  1.6  christos 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    119  1.6  christos 		       CTL_CREATE, CTL_EOL);
    120  1.6  christos 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    121  1.6  christos 		       CTLFLAG_PERMANENT,
    122  1.6  christos 		       CTLTYPE_QUAD, "sbsize",
    123  1.6  christos 		       SYSCTL_DESCR("Socket buffers used for the current user"),
    124  1.6  christos 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    125  1.6  christos 		       CTL_CREATE, CTL_EOL);
    126  1.6  christos }
    127  1.6  christos 
    128  1.1     pooka void
    129  1.1     pooka uid_init(void)
    130  1.1     pooka {
    131  1.1     pooka 
    132  1.1     pooka 	/*
    133  1.1     pooka 	 * In case of MP system, SLIST_FOREACH would force a cache line
    134  1.1     pooka 	 * write-back for every modified 'uidinfo', thus we try to keep the
    135  1.1     pooka 	 * lists short.
    136  1.1     pooka 	 */
    137  1.5        ad 	const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64);
    138  1.1     pooka 
    139  1.1     pooka 	uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash);
    140  1.1     pooka 
    141  1.1     pooka 	/*
    142  1.1     pooka 	 * Ensure that uid 0 is always in the user hash table, as
    143  1.1     pooka 	 * sbreserve() expects it available from interrupt context.
    144  1.1     pooka 	 */
    145  1.1     pooka 	(void)uid_find(0);
    146  1.6  christos 	sysctl_kern_uidinfo_setup();
    147  1.1     pooka }
    148  1.1     pooka 
    149  1.1     pooka struct uidinfo *
    150  1.1     pooka uid_find(uid_t uid)
    151  1.1     pooka {
    152  1.1     pooka 	struct uidinfo *uip, *uip_first, *newuip;
    153  1.1     pooka 	struct uihashhead *uipp;
    154  1.1     pooka 
    155  1.1     pooka 	uipp = UIHASH(uid);
    156  1.1     pooka 	newuip = NULL;
    157  1.1     pooka 
    158  1.1     pooka 	/*
    159  1.1     pooka 	 * To make insertion atomic, abstraction of SLIST will be violated.
    160  1.1     pooka 	 */
    161  1.1     pooka 	uip_first = uipp->slh_first;
    162  1.1     pooka  again:
    163  1.1     pooka 	SLIST_FOREACH(uip, uipp, ui_hash) {
    164  1.1     pooka 		if (uip->ui_uid != uid)
    165  1.1     pooka 			continue;
    166  1.2        ad 		if (newuip != NULL)
    167  1.1     pooka 			kmem_free(newuip, sizeof(*newuip));
    168  1.1     pooka 		return uip;
    169  1.1     pooka 	}
    170  1.2        ad 	if (newuip == NULL)
    171  1.1     pooka 		newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP);
    172  1.1     pooka 	newuip->ui_uid = uid;
    173  1.1     pooka 
    174  1.1     pooka 	/*
    175  1.1     pooka 	 * If atomic insert is unsuccessful, another thread might be
    176  1.1     pooka 	 * allocated this 'uid', thus full re-check is needed.
    177  1.1     pooka 	 */
    178  1.1     pooka 	newuip->ui_hash.sle_next = uip_first;
    179  1.1     pooka 	membar_producer();
    180  1.1     pooka 	uip = atomic_cas_ptr(&uipp->slh_first, uip_first, newuip);
    181  1.1     pooka 	if (uip != uip_first) {
    182  1.1     pooka 		uip_first = uip;
    183  1.1     pooka 		goto again;
    184  1.1     pooka 	}
    185  1.1     pooka 
    186  1.1     pooka 	return newuip;
    187  1.1     pooka }
    188  1.1     pooka 
    189  1.1     pooka /*
    190  1.1     pooka  * Change the count associated with number of processes
    191  1.1     pooka  * a given user is using.
    192  1.1     pooka  */
    193  1.1     pooka int
    194  1.1     pooka chgproccnt(uid_t uid, int diff)
    195  1.1     pooka {
    196  1.1     pooka 	struct uidinfo *uip;
    197  1.1     pooka 	long proccnt;
    198  1.1     pooka 
    199  1.1     pooka 	uip = uid_find(uid);
    200  1.1     pooka 	proccnt = atomic_add_long_nv(&uip->ui_proccnt, diff);
    201  1.1     pooka 	KASSERT(proccnt >= 0);
    202  1.1     pooka 	return proccnt;
    203  1.1     pooka }
    204  1.1     pooka 
    205  1.6  christos /*
    206  1.6  christos  * Change the count associated with number of lwps
    207  1.6  christos  * a given user is using.
    208  1.6  christos  */
    209  1.6  christos int
    210  1.6  christos chglwpcnt(uid_t uid, int diff)
    211  1.6  christos {
    212  1.6  christos 	struct uidinfo *uip;
    213  1.6  christos 	long lwpcnt;
    214  1.6  christos 
    215  1.6  christos 	uip = uid_find(uid);
    216  1.6  christos 	lwpcnt = atomic_add_long_nv(&uip->ui_lwpcnt, diff);
    217  1.6  christos 	KASSERT(lwpcnt >= 0);
    218  1.6  christos 	return lwpcnt;
    219  1.6  christos }
    220  1.6  christos 
    221  1.1     pooka int
    222  1.1     pooka chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
    223  1.1     pooka {
    224  1.1     pooka 	rlim_t nsb;
    225  1.2        ad 	const long diff = to - *hiwat;
    226  1.1     pooka 
    227  1.3        ad 	nsb = (rlim_t)atomic_add_long_nv((long *)&uip->ui_sbsize, diff);
    228  1.3        ad 	if (diff > 0 && nsb > xmax) {
    229  1.2        ad 		atomic_add_long((long *)&uip->ui_sbsize, -diff);
    230  1.1     pooka 		return 0;
    231  1.1     pooka 	}
    232  1.1     pooka 	*hiwat = to;
    233  1.1     pooka 	return 1;
    234  1.1     pooka }
    235