Home | History | Annotate | Line # | Download | only in kern
kern_uidinfo.c revision 1.5.14.1
      1  1.5.14.1   yamt /*	$NetBSD: kern_uidinfo.c,v 1.5.14.1 2012/10/30 17:22:32 yamt 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.5.14.1   yamt __KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.5.14.1 2012/10/30 17:22:32 yamt 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.5.14.1   yamt #include <sys/sysctl.h>
     47  1.5.14.1   yamt #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.5.14.1   yamt static int
     56  1.5.14.1   yamt sysctl_kern_uidinfo_cnt(SYSCTLFN_ARGS)
     57  1.5.14.1   yamt {
     58  1.5.14.1   yamt 	static const struct {
     59  1.5.14.1   yamt 		const char *name;
     60  1.5.14.1   yamt 		u_int value;
     61  1.5.14.1   yamt 	} nv[] = {
     62  1.5.14.1   yamt #define _MEM(n) { # n, offsetof(struct uidinfo, ui_ ## n) }
     63  1.5.14.1   yamt 		_MEM(proccnt),
     64  1.5.14.1   yamt 		_MEM(lwpcnt),
     65  1.5.14.1   yamt 		_MEM(lockcnt),
     66  1.5.14.1   yamt 		_MEM(sbsize),
     67  1.5.14.1   yamt #undef _MEM
     68  1.5.14.1   yamt 	};
     69  1.5.14.1   yamt 
     70  1.5.14.1   yamt 	for (size_t i = 0; i < __arraycount(nv); i++)
     71  1.5.14.1   yamt 		if (strcmp(nv[i].name, rnode->sysctl_name) == 0) {
     72  1.5.14.1   yamt 			uint64_t cnt;
     73  1.5.14.1   yamt 			struct sysctlnode node = *rnode;
     74  1.5.14.1   yamt 			struct uidinfo *uip;
     75  1.5.14.1   yamt 
     76  1.5.14.1   yamt 			node.sysctl_data = &cnt;
     77  1.5.14.1   yamt 			uip = uid_find(kauth_cred_geteuid(l->l_cred));
     78  1.5.14.1   yamt 
     79  1.5.14.1   yamt 			*(uint64_t *)node.sysctl_data =
     80  1.5.14.1   yamt 			    *(u_long *)((char *)uip + nv[i].value);
     81  1.5.14.1   yamt 
     82  1.5.14.1   yamt 			return sysctl_lookup(SYSCTLFN_CALL(&node));
     83  1.5.14.1   yamt 		}
     84  1.5.14.1   yamt 
     85  1.5.14.1   yamt 	return EINVAL;
     86  1.5.14.1   yamt }
     87  1.5.14.1   yamt 
     88  1.5.14.1   yamt static void
     89  1.5.14.1   yamt sysctl_kern_uidinfo_setup(void)
     90  1.5.14.1   yamt {
     91  1.5.14.1   yamt 	const struct sysctlnode *rnode, *cnode;
     92  1.5.14.1   yamt 	struct sysctllog *kern_uidinfo_sysctllog;
     93  1.5.14.1   yamt 
     94  1.5.14.1   yamt 	kern_uidinfo_sysctllog = NULL;
     95  1.5.14.1   yamt 	sysctl_createv(&kern_uidinfo_sysctllog, 0, NULL, &rnode,
     96  1.5.14.1   yamt 		       CTLFLAG_PERMANENT,
     97  1.5.14.1   yamt 		       CTLTYPE_NODE, "uidinfo",
     98  1.5.14.1   yamt 		       SYSCTL_DESCR("Resource usage per uid"),
     99  1.5.14.1   yamt 		       NULL, 0, NULL, 0,
    100  1.5.14.1   yamt 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    101  1.5.14.1   yamt 
    102  1.5.14.1   yamt 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    103  1.5.14.1   yamt 		       CTLFLAG_PERMANENT,
    104  1.5.14.1   yamt 		       CTLTYPE_QUAD, "proccnt",
    105  1.5.14.1   yamt 		       SYSCTL_DESCR("Number of processes for the current user"),
    106  1.5.14.1   yamt 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    107  1.5.14.1   yamt 		       CTL_CREATE, CTL_EOL);
    108  1.5.14.1   yamt 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    109  1.5.14.1   yamt 		       CTLFLAG_PERMANENT,
    110  1.5.14.1   yamt 		       CTLTYPE_QUAD, "lwpcnt",
    111  1.5.14.1   yamt 		       SYSCTL_DESCR("Number of lwps for the current user"),
    112  1.5.14.1   yamt 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    113  1.5.14.1   yamt 		       CTL_CREATE, CTL_EOL);
    114  1.5.14.1   yamt 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    115  1.5.14.1   yamt 		       CTLFLAG_PERMANENT,
    116  1.5.14.1   yamt 		       CTLTYPE_QUAD, "lockcnt",
    117  1.5.14.1   yamt 		       SYSCTL_DESCR("Number of locks for the current user"),
    118  1.5.14.1   yamt 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    119  1.5.14.1   yamt 		       CTL_CREATE, CTL_EOL);
    120  1.5.14.1   yamt 	sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
    121  1.5.14.1   yamt 		       CTLFLAG_PERMANENT,
    122  1.5.14.1   yamt 		       CTLTYPE_QUAD, "sbsize",
    123  1.5.14.1   yamt 		       SYSCTL_DESCR("Socket buffers used for the current user"),
    124  1.5.14.1   yamt 		       sysctl_kern_uidinfo_cnt, 0, NULL, 0,
    125  1.5.14.1   yamt 		       CTL_CREATE, CTL_EOL);
    126  1.5.14.1   yamt }
    127  1.5.14.1   yamt 
    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.5.14.1   yamt 	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.5.14.1   yamt /*
    206  1.5.14.1   yamt  * Change the count associated with number of lwps
    207  1.5.14.1   yamt  * a given user is using.
    208  1.5.14.1   yamt  */
    209  1.5.14.1   yamt int
    210  1.5.14.1   yamt chglwpcnt(uid_t uid, int diff)
    211  1.5.14.1   yamt {
    212  1.5.14.1   yamt 	struct uidinfo *uip;
    213  1.5.14.1   yamt 	long lwpcnt;
    214  1.5.14.1   yamt 
    215  1.5.14.1   yamt 	uip = uid_find(uid);
    216  1.5.14.1   yamt 	lwpcnt = atomic_add_long_nv(&uip->ui_lwpcnt, diff);
    217  1.5.14.1   yamt 	KASSERT(lwpcnt >= 0);
    218  1.5.14.1   yamt 	return lwpcnt;
    219  1.5.14.1   yamt }
    220  1.5.14.1   yamt 
    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