2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/refcount.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysent.h>
61 #include <vm/vm_param.h>
63 #include <vm/vm_map.h>
66 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
67 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
68 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
69 static struct mtx uihashtbl_mtx;
70 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
71 static u_long uihash; /* size of hash table - 1 */
73 static void calcru1(struct proc *p, struct rusage_ext *ruxp,
74 struct timeval *up, struct timeval *sp);
75 static int donice(struct thread *td, struct proc *chgp, int n);
76 static struct uidinfo *uilookup(uid_t uid);
79 * Resource controls and accounting.
82 #ifndef _SYS_SYSPROTO_H_
83 struct getpriority_args {
94 register struct getpriority_args *uap;
102 switch (uap->which) {
106 low = td->td_proc->p_nice;
111 if (p_cansee(td, p) == 0)
118 sx_slock(&proctree_lock);
120 pg = td->td_proc->p_pgrp;
123 pg = pgfind(uap->who);
125 sx_sunlock(&proctree_lock);
129 sx_sunlock(&proctree_lock);
130 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
132 if (!p_cansee(td, p)) {
143 uap->who = td->td_ucred->cr_uid;
144 sx_slock(&allproc_lock);
145 FOREACH_PROC_IN_SYSTEM(p) {
147 if (!p_cansee(td, p) &&
148 p->p_ucred->cr_uid == uap->who) {
154 sx_sunlock(&allproc_lock);
161 if (low == PRIO_MAX + 1 && error == 0)
163 td->td_retval[0] = low;
167 #ifndef _SYS_SYSPROTO_H_
168 struct setpriority_args {
180 struct setpriority_args *uap;
182 struct proc *curp, *p;
184 int found = 0, error = 0;
187 switch (uap->which) {
191 error = donice(td, curp, uap->prio);
197 if (p_cansee(td, p) == 0)
198 error = donice(td, p, uap->prio);
205 sx_slock(&proctree_lock);
210 pg = pgfind(uap->who);
212 sx_sunlock(&proctree_lock);
216 sx_sunlock(&proctree_lock);
217 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
219 if (!p_cansee(td, p)) {
220 error = donice(td, p, uap->prio);
230 uap->who = td->td_ucred->cr_uid;
231 sx_slock(&allproc_lock);
232 FOREACH_PROC_IN_SYSTEM(p) {
234 if (p->p_ucred->cr_uid == uap->who &&
236 error = donice(td, p, uap->prio);
241 sx_sunlock(&allproc_lock);
248 if (found == 0 && error == 0)
254 * Set "nice" for a (whole) process.
257 donice(struct thread *td, struct proc *p, int n)
261 PROC_LOCK_ASSERT(p, MA_OWNED);
262 if ((error = p_cansched(td, p)))
268 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
270 mtx_lock_spin(&sched_lock);
272 mtx_unlock_spin(&sched_lock);
277 * Set realtime priority for LWP.
281 #ifndef _SYS_SYSPROTO_H_
282 struct rtprio_thread_args {
290 rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
298 /* Perform copyin before acquiring locks if needed. */
299 if (uap->function == RTP_SET)
300 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
306 * Though lwpid is unique, only current process is supported
307 * since there is no efficient way to look up a LWP yet.
312 switch (uap->function) {
314 if ((error = p_cansee(td, p)))
316 mtx_lock_spin(&sched_lock);
317 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
320 td1 = thread_find(p, uap->lwpid);
322 pri_to_rtp(td1, &rtp);
325 mtx_unlock_spin(&sched_lock);
327 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
329 if ((error = p_cansched(td, p)) || (error = cierror))
332 /* Disallow setting rtprio in most cases if not superuser. */
333 if (suser(td) != 0) {
334 /* can't set realtime priority */
336 * Realtime priority has to be restricted for reasons which should be
337 * obvious. However, for idle priority, there is a potential for
338 * system deadlock if an idleprio process gains a lock on a resource
339 * that other processes need (and the idleprio process can't run
340 * due to a CPU-bound normal process). Fix me! XXX
343 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
345 if (rtp.type != RTP_PRIO_NORMAL) {
352 mtx_lock_spin(&sched_lock);
353 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
356 td1 = thread_find(p, uap->lwpid);
358 error = rtp_to_pri(&rtp, td1);
361 mtx_unlock_spin(&sched_lock);
372 * Set realtime priority.
376 #ifndef _SYS_SYSPROTO_H_
386 struct thread *td; /* curthread */
387 register struct rtprio_args *uap;
395 /* Perform copyin before acquiring locks if needed. */
396 if (uap->function == RTP_SET)
397 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
411 switch (uap->function) {
413 if ((error = p_cansee(td, p)))
415 mtx_lock_spin(&sched_lock);
417 * Return OUR priority if no pid specified,
418 * or if one is, report the highest priority
419 * in the process. There isn't much more you can do as
420 * there is only room to return a single priority.
421 * XXXKSE: maybe need a new interface to report
422 * priorities of multiple system scope threads.
423 * Note: specifying our own pid is not the same
424 * as leaving it zero.
427 pri_to_rtp(td, &rtp);
431 rtp.type = RTP_PRIO_IDLE;
432 rtp.prio = RTP_PRIO_MAX;
433 FOREACH_THREAD_IN_PROC(p, tdp) {
434 pri_to_rtp(tdp, &rtp2);
435 if (rtp2.type < rtp.type ||
436 (rtp2.type == rtp.type &&
437 rtp2.prio < rtp.prio)) {
438 rtp.type = rtp2.type;
439 rtp.prio = rtp2.prio;
443 mtx_unlock_spin(&sched_lock);
445 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
447 if ((error = p_cansched(td, p)) || (error = cierror))
450 /* Disallow setting rtprio in most cases if not superuser. */
451 if (priv_check(td, PRIV_SCHED_RTPRIO) != 0) {
452 /* can't set someone else's */
457 /* can't set realtime priority */
459 * Realtime priority has to be restricted for reasons which should be
460 * obvious. However, for idle priority, there is a potential for
461 * system deadlock if an idleprio process gains a lock on a resource
462 * that other processes need (and the idleprio process can't run
463 * due to a CPU-bound normal process). Fix me! XXX
466 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
468 if (rtp.type != RTP_PRIO_NORMAL) {
476 * If we are setting our own priority, set just our
477 * thread but if we are doing another process,
478 * do all the threads on that process. If we
479 * specify our own pid we do the latter.
481 mtx_lock_spin(&sched_lock);
483 error = rtp_to_pri(&rtp, td);
485 FOREACH_THREAD_IN_PROC(p, td) {
486 if ((error = rtp_to_pri(&rtp, td)) != 0)
490 mtx_unlock_spin(&sched_lock);
501 rtp_to_pri(struct rtprio *rtp, struct thread *td)
505 mtx_assert(&sched_lock, MA_OWNED);
506 if (rtp->prio > RTP_PRIO_MAX)
508 switch (RTP_PRIO_BASE(rtp->type)) {
509 case RTP_PRIO_REALTIME:
510 newpri = PRI_MIN_REALTIME + rtp->prio;
512 case RTP_PRIO_NORMAL:
513 newpri = PRI_MIN_TIMESHARE + rtp->prio;
516 newpri = PRI_MIN_IDLE + rtp->prio;
521 sched_class(td, rtp->type); /* XXX fix */
522 sched_user_prio(td, newpri);
524 sched_prio(curthread, td->td_user_pri); /* XXX dubious */
529 pri_to_rtp(struct thread *td, struct rtprio *rtp)
532 mtx_assert(&sched_lock, MA_OWNED);
533 switch (PRI_BASE(td->td_pri_class)) {
535 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
538 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
541 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
546 rtp->type = td->td_pri_class;
549 #if defined(COMPAT_43)
550 #ifndef _SYS_SYSPROTO_H_
551 struct osetrlimit_args {
562 register struct osetrlimit_args *uap;
568 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
570 lim.rlim_cur = olim.rlim_cur;
571 lim.rlim_max = olim.rlim_max;
572 error = kern_setrlimit(td, uap->which, &lim);
576 #ifndef _SYS_SYSPROTO_H_
577 struct ogetrlimit_args {
588 register struct ogetrlimit_args *uap;
595 if (uap->which >= RLIM_NLIMITS)
599 lim_rlimit(p, uap->which, &rl);
603 * XXX would be more correct to convert only RLIM_INFINITY to the
604 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
605 * values. Most 64->32 and 32->16 conversions, including not
606 * unimportant ones of uids are even more broken than what we
607 * do here (they blindly truncate). We don't do this correctly
608 * here since we have little experience with EOVERFLOW yet.
609 * Elsewhere, getuid() can't fail...
611 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
612 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
613 error = copyout(&olim, uap->rlp, sizeof(olim));
616 #endif /* COMPAT_43 */
618 #ifndef _SYS_SYSPROTO_H_
619 struct __setrlimit_args {
630 register struct __setrlimit_args *uap;
635 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
637 error = kern_setrlimit(td, uap->which, &alim);
642 kern_setrlimit(td, which, limp)
647 struct plimit *newlim, *oldlim;
649 register struct rlimit *alimp;
653 if (which >= RLIM_NLIMITS)
657 * Preserve historical bugs by treating negative limits as unsigned.
659 if (limp->rlim_cur < 0)
660 limp->rlim_cur = RLIM_INFINITY;
661 if (limp->rlim_max < 0)
662 limp->rlim_max = RLIM_INFINITY;
666 newlim = lim_alloc();
669 alimp = &oldlim->pl_rlimit[which];
670 if (limp->rlim_cur > alimp->rlim_max ||
671 limp->rlim_max > alimp->rlim_max)
672 if ((error = priv_check_cred(td->td_ucred,
673 PRIV_PROC_SETRLIMIT, SUSER_ALLOWJAIL))) {
678 if (limp->rlim_cur > limp->rlim_max)
679 limp->rlim_cur = limp->rlim_max;
680 lim_copy(newlim, oldlim);
681 alimp = &newlim->pl_rlimit[which];
686 mtx_lock_spin(&sched_lock);
687 p->p_cpulimit = limp->rlim_cur;
688 mtx_unlock_spin(&sched_lock);
691 if (limp->rlim_cur > maxdsiz)
692 limp->rlim_cur = maxdsiz;
693 if (limp->rlim_max > maxdsiz)
694 limp->rlim_max = maxdsiz;
698 if (limp->rlim_cur > maxssiz)
699 limp->rlim_cur = maxssiz;
700 if (limp->rlim_max > maxssiz)
701 limp->rlim_max = maxssiz;
702 oldssiz = alimp->rlim_cur;
706 if (limp->rlim_cur > maxfilesperproc)
707 limp->rlim_cur = maxfilesperproc;
708 if (limp->rlim_max > maxfilesperproc)
709 limp->rlim_max = maxfilesperproc;
713 if (limp->rlim_cur > maxprocperuid)
714 limp->rlim_cur = maxprocperuid;
715 if (limp->rlim_max > maxprocperuid)
716 limp->rlim_max = maxprocperuid;
717 if (limp->rlim_cur < 1)
719 if (limp->rlim_max < 1)
728 if (which == RLIMIT_STACK) {
730 * Stack is allocated to the max at exec time with only
731 * "rlim_cur" bytes accessible. If stack limit is going
732 * up make more accessible, if going down make inaccessible.
734 if (limp->rlim_cur != oldssiz) {
739 if (limp->rlim_cur > oldssiz) {
740 prot = p->p_sysent->sv_stackprot;
741 size = limp->rlim_cur - oldssiz;
742 addr = p->p_sysent->sv_usrstack -
746 size = oldssiz - limp->rlim_cur;
747 addr = p->p_sysent->sv_usrstack - oldssiz;
749 addr = trunc_page(addr);
750 size = round_page(size);
751 (void)vm_map_protect(&p->p_vmspace->vm_map,
752 addr, addr + size, prot, FALSE);
757 * The data size limit may need to be changed to a value
758 * that makes sense for the 32 bit binary.
760 if (p->p_sysent->sv_fixlimits != NULL)
761 p->p_sysent->sv_fixlimits(p);
765 #ifndef _SYS_SYSPROTO_H_
766 struct __getrlimit_args {
778 register struct __getrlimit_args *uap;
784 if (uap->which >= RLIM_NLIMITS)
788 lim_rlimit(p, uap->which, &rlim);
790 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
795 * Transform the running time and tick information for children of proc p
796 * into user and system time usage.
805 PROC_LOCK_ASSERT(p, MA_OWNED);
806 calcru1(p, &p->p_crux, up, sp);
810 * Transform the running time and tick information in proc p into user
811 * and system time usage. If appropriate, include the current time slice
815 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
817 struct rusage_ext rux;
821 PROC_LOCK_ASSERT(p, MA_OWNED);
822 mtx_assert(&sched_lock, MA_NOTOWNED);
823 mtx_lock_spin(&sched_lock);
826 * If we are getting stats for the current process, then add in the
827 * stats that this thread has accumulated in its current time slice.
828 * We reset the thread and CPU state as if we had performed a context
831 if (curthread->td_proc == p) {
834 p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
835 PCPU_SET(switchtime, u);
836 p->p_rux.rux_uticks += td->td_uticks;
838 p->p_rux.rux_iticks += td->td_iticks;
840 p->p_rux.rux_sticks += td->td_sticks;
843 /* Work on a copy of p_rux so we can let go of sched_lock */
845 mtx_unlock_spin(&sched_lock);
846 calcru1(p, &rux, up, sp);
847 /* Update the result from the p_rux copy */
848 p->p_rux.rux_uu = rux.rux_uu;
849 p->p_rux.rux_su = rux.rux_su;
850 p->p_rux.rux_tu = rux.rux_tu;
854 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
857 /* {user, system, interrupt, total} {ticks, usec}: */
858 u_int64_t ut, uu, st, su, it, tt, tu;
860 ut = ruxp->rux_uticks;
861 st = ruxp->rux_sticks;
862 it = ruxp->rux_iticks;
865 /* Avoid divide by zero */
869 tu = cputick2usec(ruxp->rux_runtime);
870 if ((int64_t)tu < 0) {
871 /* XXX: this should be an assert /phk */
872 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
873 (intmax_t)tu, p->p_pid, p->p_comm);
877 if (tu >= ruxp->rux_tu) {
879 * The normal case, time increased.
880 * Enforce monotonicity of bucketed numbers.
883 if (uu < ruxp->rux_uu)
886 if (su < ruxp->rux_su)
888 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
890 * When we calibrate the cputicker, it is not uncommon to
891 * see the presumably fixed frequency increase slightly over
892 * time as a result of thermal stabilization and NTP
893 * discipline (of the reference clock). We therefore ignore
894 * a bit of backwards slop because we expect to catch up
895 * shortly. We use a 3 microsecond limit to catch low
896 * counts and a 1% limit for high counts.
901 } else { /* tu < ruxp->rux_tu */
903 * What happene here was likely that a laptop, which ran at
904 * a reduced clock frequency at boot, kicked into high gear.
905 * The wisdom of spamming this message in that case is
906 * dubious, but it might also be indicative of something
907 * serious, so lets keep it and hope laptops can be made
908 * more truthful about their CPU speed via ACPI.
910 printf("calcru: runtime went backwards from %ju usec "
911 "to %ju usec for pid %d (%s)\n",
912 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
913 p->p_pid, p->p_comm);
922 up->tv_sec = uu / 1000000;
923 up->tv_usec = uu % 1000000;
924 sp->tv_sec = su / 1000000;
925 sp->tv_usec = su % 1000000;
928 #ifndef _SYS_SYSPROTO_H_
929 struct getrusage_args {
931 struct rusage *rusage;
939 register struct thread *td;
940 register struct getrusage_args *uap;
945 error = kern_getrusage(td, uap->who, &ru);
947 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
952 kern_getrusage(td, who, rup)
964 *rup = p->p_stats->p_ru;
965 calcru(p, &rup->ru_utime, &rup->ru_stime);
968 case RUSAGE_CHILDREN:
969 *rup = p->p_stats->p_cru;
970 calccru(p, &rup->ru_utime, &rup->ru_stime);
982 ruadd(ru, rux, ru2, rux2)
984 struct rusage_ext *rux;
986 struct rusage_ext *rux2;
988 register long *ip, *ip2;
991 rux->rux_runtime += rux2->rux_runtime;
992 rux->rux_uticks += rux2->rux_uticks;
993 rux->rux_sticks += rux2->rux_sticks;
994 rux->rux_iticks += rux2->rux_iticks;
995 rux->rux_uu += rux2->rux_uu;
996 rux->rux_su += rux2->rux_su;
997 rux->rux_tu += rux2->rux_tu;
998 if (ru->ru_maxrss < ru2->ru_maxrss)
999 ru->ru_maxrss = ru2->ru_maxrss;
1001 ip2 = &ru2->ru_first;
1002 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1007 * Allocate a new resource limits structure and initialize its
1008 * reference count and mutex pointer.
1013 struct plimit *limp;
1015 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1016 refcount_init(&limp->pl_refcnt, 1);
1022 struct plimit *limp;
1025 refcount_acquire(&limp->pl_refcnt);
1031 struct plimit *limp;
1034 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1035 if (refcount_release(&limp->pl_refcnt))
1036 free((void *)limp, M_PLIMIT);
1040 * Make a copy of the plimit structure.
1041 * We share these structures copy-on-write after fork.
1045 struct plimit *dst, *src;
1048 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1049 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1053 * Return the hard limit for a particular system resource. The
1054 * which parameter specifies the index into the rlimit array.
1057 lim_max(struct proc *p, int which)
1061 lim_rlimit(p, which, &rl);
1062 return (rl.rlim_max);
1066 * Return the current (soft) limit for a particular system resource.
1067 * The which parameter which specifies the index into the rlimit array
1070 lim_cur(struct proc *p, int which)
1074 lim_rlimit(p, which, &rl);
1075 return (rl.rlim_cur);
1079 * Return a copy of the entire rlimit structure for the system limit
1080 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1083 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1086 PROC_LOCK_ASSERT(p, MA_OWNED);
1087 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1088 ("request for invalid resource limit"));
1089 *rlp = p->p_limit->pl_rlimit[which];
1093 * Find the uidinfo structure for a uid. This structure is used to
1094 * track the total resource consumption (process count, socket buffer
1095 * size, etc.) for the uid and impose limits.
1101 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1102 mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF);
1106 * Look up a uidinfo struct for the parameter uid.
1107 * uihashtbl_mtx must be locked.
1109 static struct uidinfo *
1113 struct uihashhead *uipp;
1114 struct uidinfo *uip;
1116 mtx_assert(&uihashtbl_mtx, MA_OWNED);
1118 LIST_FOREACH(uip, uipp, ui_hash)
1119 if (uip->ui_uid == uid)
1126 * Find or allocate a struct uidinfo for a particular uid.
1127 * Increase refcount on uidinfo struct returned.
1128 * uifree() should be called on a struct uidinfo when released.
1134 struct uidinfo *old_uip, *uip;
1136 mtx_lock(&uihashtbl_mtx);
1137 uip = uilookup(uid);
1139 mtx_unlock(&uihashtbl_mtx);
1140 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1141 mtx_lock(&uihashtbl_mtx);
1143 * There's a chance someone created our uidinfo while we
1144 * were in malloc and not holding the lock, so we have to
1145 * make sure we don't insert a duplicate uidinfo.
1147 if ((old_uip = uilookup(uid)) != NULL) {
1148 /* Someone else beat us to it. */
1149 free(uip, M_UIDINFO);
1152 uip->ui_mtxp = mtx_pool_alloc(mtxpool_sleep);
1154 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1158 mtx_unlock(&uihashtbl_mtx);
1163 * Place another refcount on a uidinfo struct.
1167 struct uidinfo *uip;
1172 UIDINFO_UNLOCK(uip);
1176 * Since uidinfo structs have a long lifetime, we use an
1177 * opportunistic refcounting scheme to avoid locking the lookup hash
1180 * If the refcount hits 0, we need to free the structure,
1181 * which means we need to lock the hash.
1183 * After locking the struct and lowering the refcount, if we find
1184 * that we don't need to free, simply unlock and return.
1186 * If refcount lowering results in need to free, bump the count
1187 * back up, lose the lock and aquire the locks in the proper
1188 * order to try again.
1192 struct uidinfo *uip;
1195 /* Prepare for optimal case. */
1198 if (--uip->ui_ref != 0) {
1199 UIDINFO_UNLOCK(uip);
1203 /* Prepare for suboptimal case. */
1205 UIDINFO_UNLOCK(uip);
1206 mtx_lock(&uihashtbl_mtx);
1210 * We must subtract one from the count again because we backed out
1211 * our initial subtraction before dropping the lock.
1212 * Since another thread may have added a reference after we dropped the
1213 * initial lock we have to test for zero again.
1215 if (--uip->ui_ref == 0) {
1216 LIST_REMOVE(uip, ui_hash);
1217 mtx_unlock(&uihashtbl_mtx);
1218 if (uip->ui_sbsize != 0)
1219 printf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1220 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1221 if (uip->ui_proccnt != 0)
1222 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1223 uip->ui_uid, uip->ui_proccnt);
1224 UIDINFO_UNLOCK(uip);
1225 FREE(uip, M_UIDINFO);
1229 mtx_unlock(&uihashtbl_mtx);
1230 UIDINFO_UNLOCK(uip);
1234 * Change the count associated with number of processes
1235 * a given user is using. When 'max' is 0, don't enforce a limit
1238 chgproccnt(uip, diff, max)
1239 struct uidinfo *uip;
1245 /* Don't allow them to exceed max, but allow subtraction. */
1246 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1247 UIDINFO_UNLOCK(uip);
1250 uip->ui_proccnt += diff;
1251 if (uip->ui_proccnt < 0)
1252 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1253 UIDINFO_UNLOCK(uip);
1258 * Change the total socket buffer size a user has used.
1261 chgsbsize(uip, hiwat, to, max)
1262 struct uidinfo *uip;
1270 new = uip->ui_sbsize + to - *hiwat;
1271 /* Don't allow them to exceed max, but allow subtraction. */
1272 if (to > *hiwat && new > max) {
1273 UIDINFO_UNLOCK(uip);
1276 uip->ui_sbsize = new;
1277 UIDINFO_UNLOCK(uip);
1280 printf("negative sbsize for uid = %d\n", uip->ui_uid);