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