Home | History | Annotate | Line # | Download | only in kern
kern_uidinfo.c revision 1.3.4.2
      1  1.3.4.2  skrll /*	$NetBSD: kern_uidinfo.c,v 1.3.4.2 2009/04/28 07:37:00 skrll 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.3.4.2  skrll __KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.3.4.2 2009/04/28 07:37:00 skrll 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.3.4.2  skrll #include <sys/cpu.h>
     47      1.1  pooka 
     48      1.1  pooka static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl;
     49      1.1  pooka static u_long 		uihash;
     50      1.1  pooka 
     51      1.1  pooka #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
     52      1.1  pooka 
     53      1.1  pooka void
     54      1.1  pooka uid_init(void)
     55      1.1  pooka {
     56      1.1  pooka 
     57      1.1  pooka 	/*
     58      1.1  pooka 	 * In case of MP system, SLIST_FOREACH would force a cache line
     59      1.1  pooka 	 * write-back for every modified 'uidinfo', thus we try to keep the
     60      1.1  pooka 	 * lists short.
     61      1.1  pooka 	 */
     62  1.3.4.2  skrll 	const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64);
     63      1.1  pooka 
     64      1.1  pooka 	uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash);
     65      1.1  pooka 
     66      1.1  pooka 	/*
     67      1.1  pooka 	 * Ensure that uid 0 is always in the user hash table, as
     68      1.1  pooka 	 * sbreserve() expects it available from interrupt context.
     69      1.1  pooka 	 */
     70      1.1  pooka 	(void)uid_find(0);
     71      1.1  pooka }
     72      1.1  pooka 
     73      1.1  pooka struct uidinfo *
     74      1.1  pooka uid_find(uid_t uid)
     75      1.1  pooka {
     76      1.1  pooka 	struct uidinfo *uip, *uip_first, *newuip;
     77      1.1  pooka 	struct uihashhead *uipp;
     78      1.1  pooka 
     79      1.1  pooka 	uipp = UIHASH(uid);
     80      1.1  pooka 	newuip = NULL;
     81      1.1  pooka 
     82      1.1  pooka 	/*
     83      1.1  pooka 	 * To make insertion atomic, abstraction of SLIST will be violated.
     84      1.1  pooka 	 */
     85      1.1  pooka 	uip_first = uipp->slh_first;
     86      1.1  pooka  again:
     87      1.1  pooka 	SLIST_FOREACH(uip, uipp, ui_hash) {
     88      1.1  pooka 		if (uip->ui_uid != uid)
     89      1.1  pooka 			continue;
     90      1.2     ad 		if (newuip != NULL)
     91      1.1  pooka 			kmem_free(newuip, sizeof(*newuip));
     92      1.1  pooka 		return uip;
     93      1.1  pooka 	}
     94      1.2     ad 	if (newuip == NULL)
     95      1.1  pooka 		newuip = kmem_zalloc(sizeof(*newuip), KM_SLEEP);
     96      1.1  pooka 	newuip->ui_uid = uid;
     97      1.1  pooka 
     98      1.1  pooka 	/*
     99      1.1  pooka 	 * If atomic insert is unsuccessful, another thread might be
    100      1.1  pooka 	 * allocated this 'uid', thus full re-check is needed.
    101      1.1  pooka 	 */
    102      1.1  pooka 	newuip->ui_hash.sle_next = uip_first;
    103      1.1  pooka 	membar_producer();
    104      1.1  pooka 	uip = atomic_cas_ptr(&uipp->slh_first, uip_first, newuip);
    105      1.1  pooka 	if (uip != uip_first) {
    106      1.1  pooka 		uip_first = uip;
    107      1.1  pooka 		goto again;
    108      1.1  pooka 	}
    109      1.1  pooka 
    110      1.1  pooka 	return newuip;
    111      1.1  pooka }
    112      1.1  pooka 
    113      1.1  pooka /*
    114      1.1  pooka  * Change the count associated with number of processes
    115      1.1  pooka  * a given user is using.
    116      1.1  pooka  */
    117      1.1  pooka int
    118      1.1  pooka chgproccnt(uid_t uid, int diff)
    119      1.1  pooka {
    120      1.1  pooka 	struct uidinfo *uip;
    121      1.1  pooka 	long proccnt;
    122      1.1  pooka 
    123      1.1  pooka 	uip = uid_find(uid);
    124      1.1  pooka 	proccnt = atomic_add_long_nv(&uip->ui_proccnt, diff);
    125      1.1  pooka 	KASSERT(proccnt >= 0);
    126      1.1  pooka 	return proccnt;
    127      1.1  pooka }
    128      1.1  pooka 
    129      1.1  pooka int
    130      1.1  pooka chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
    131      1.1  pooka {
    132      1.1  pooka 	rlim_t nsb;
    133      1.2     ad 	const long diff = to - *hiwat;
    134      1.1  pooka 
    135      1.3     ad 	nsb = (rlim_t)atomic_add_long_nv((long *)&uip->ui_sbsize, diff);
    136      1.3     ad 	if (diff > 0 && nsb > xmax) {
    137      1.2     ad 		atomic_add_long((long *)&uip->ui_sbsize, -diff);
    138      1.1  pooka 		return 0;
    139      1.1  pooka 	}
    140      1.1  pooka 	*hiwat = to;
    141      1.1  pooka 	return 1;
    142      1.1  pooka }
    143