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.
81 #ifndef _SYS_SYSPROTO_H_
82 struct getpriority_args {
90 register struct getpriority_args *uap;
102 low = td->td_proc->p_nice;
107 if (p_cansee(td, p) == 0)
114 sx_slock(&proctree_lock);
116 pg = td->td_proc->p_pgrp;
119 pg = pgfind(uap->who);
121 sx_sunlock(&proctree_lock);
125 sx_sunlock(&proctree_lock);
126 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
128 if (!p_cansee(td, p)) {
139 uap->who = td->td_ucred->cr_uid;
140 sx_slock(&allproc_lock);
141 FOREACH_PROC_IN_SYSTEM(p) {
142 /* Do not bother to check PRS_NEW processes */
143 if (p->p_state == PRS_NEW)
146 if (!p_cansee(td, p) &&
147 p->p_ucred->cr_uid == uap->who) {
153 sx_sunlock(&allproc_lock);
160 if (low == PRIO_MAX + 1 && error == 0)
162 td->td_retval[0] = low;
166 #ifndef _SYS_SYSPROTO_H_
167 struct setpriority_args {
176 struct setpriority_args *uap;
178 struct proc *curp, *p;
180 int found = 0, error = 0;
183 switch (uap->which) {
187 error = donice(td, curp, uap->prio);
193 if (p_cansee(td, p) == 0)
194 error = donice(td, p, uap->prio);
201 sx_slock(&proctree_lock);
206 pg = pgfind(uap->who);
208 sx_sunlock(&proctree_lock);
212 sx_sunlock(&proctree_lock);
213 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
215 if (!p_cansee(td, p)) {
216 error = donice(td, p, uap->prio);
226 uap->who = td->td_ucred->cr_uid;
227 sx_slock(&allproc_lock);
228 FOREACH_PROC_IN_SYSTEM(p) {
230 if (p->p_ucred->cr_uid == uap->who &&
232 error = donice(td, p, uap->prio);
237 sx_sunlock(&allproc_lock);
244 if (found == 0 && error == 0)
250 * Set "nice" for a (whole) process.
253 donice(struct thread *td, struct proc *p, int n)
257 PROC_LOCK_ASSERT(p, MA_OWNED);
258 if ((error = p_cansched(td, p)))
264 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
266 mtx_lock_spin(&sched_lock);
268 mtx_unlock_spin(&sched_lock);
273 * Set realtime priority for LWP.
275 #ifndef _SYS_SYSPROTO_H_
276 struct rtprio_thread_args {
283 rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
291 /* Perform copyin before acquiring locks if needed. */
292 if (uap->function == RTP_SET)
293 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
299 * Though lwpid is unique, only current process is supported
300 * since there is no efficient way to look up a LWP yet.
305 switch (uap->function) {
307 if ((error = p_cansee(td, p)))
309 mtx_lock_spin(&sched_lock);
310 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
313 td1 = thread_find(p, uap->lwpid);
315 pri_to_rtp(td1, &rtp);
318 mtx_unlock_spin(&sched_lock);
320 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
322 if ((error = p_cansched(td, p)) || (error = cierror))
325 /* Disallow setting rtprio in most cases if not superuser. */
326 if (priv_check(td, PRIV_SCHED_RTPRIO) != 0) {
327 /* can't set realtime priority */
329 * Realtime priority has to be restricted for reasons which should be
330 * obvious. However, for idle priority, there is a potential for
331 * system deadlock if an idleprio process gains a lock on a resource
332 * that other processes need (and the idleprio process can't run
333 * due to a CPU-bound normal process). Fix me! XXX
336 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
338 if (rtp.type != RTP_PRIO_NORMAL) {
345 mtx_lock_spin(&sched_lock);
346 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
349 td1 = thread_find(p, uap->lwpid);
351 error = rtp_to_pri(&rtp, td1);
354 mtx_unlock_spin(&sched_lock);
365 * Set realtime priority.
367 #ifndef _SYS_SYSPROTO_H_
376 struct thread *td; /* curthread */
377 register struct rtprio_args *uap;
385 /* Perform copyin before acquiring locks if needed. */
386 if (uap->function == RTP_SET)
387 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
401 switch (uap->function) {
403 if ((error = p_cansee(td, p)))
405 mtx_lock_spin(&sched_lock);
407 * Return OUR priority if no pid specified,
408 * or if one is, report the highest priority
409 * in the process. There isn't much more you can do as
410 * there is only room to return a single priority.
411 * XXXKSE: maybe need a new interface to report
412 * priorities of multiple system scope threads.
413 * Note: specifying our own pid is not the same
414 * as leaving it zero.
417 pri_to_rtp(td, &rtp);
421 rtp.type = RTP_PRIO_IDLE;
422 rtp.prio = RTP_PRIO_MAX;
423 FOREACH_THREAD_IN_PROC(p, tdp) {
424 pri_to_rtp(tdp, &rtp2);
425 if (rtp2.type < rtp.type ||
426 (rtp2.type == rtp.type &&
427 rtp2.prio < rtp.prio)) {
428 rtp.type = rtp2.type;
429 rtp.prio = rtp2.prio;
433 mtx_unlock_spin(&sched_lock);
435 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
437 if ((error = p_cansched(td, p)) || (error = cierror))
440 /* Disallow setting rtprio in most cases if not superuser. */
441 if (priv_check(td, PRIV_SCHED_RTPRIO) != 0) {
442 /* can't set someone else's */
447 /* can't set realtime priority */
449 * Realtime priority has to be restricted for reasons which should be
450 * obvious. However, for idle priority, there is a potential for
451 * system deadlock if an idleprio process gains a lock on a resource
452 * that other processes need (and the idleprio process can't run
453 * due to a CPU-bound normal process). Fix me! XXX
456 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
458 if (rtp.type != RTP_PRIO_NORMAL) {
466 * If we are setting our own priority, set just our
467 * thread but if we are doing another process,
468 * do all the threads on that process. If we
469 * specify our own pid we do the latter.
471 mtx_lock_spin(&sched_lock);
473 error = rtp_to_pri(&rtp, td);
475 FOREACH_THREAD_IN_PROC(p, td) {
476 if ((error = rtp_to_pri(&rtp, td)) != 0)
480 mtx_unlock_spin(&sched_lock);
491 rtp_to_pri(struct rtprio *rtp, struct thread *td)
495 mtx_assert(&sched_lock, MA_OWNED);
496 if (rtp->prio > RTP_PRIO_MAX)
498 switch (RTP_PRIO_BASE(rtp->type)) {
499 case RTP_PRIO_REALTIME:
500 newpri = PRI_MIN_REALTIME + rtp->prio;
502 case RTP_PRIO_NORMAL:
503 newpri = PRI_MIN_TIMESHARE + rtp->prio;
506 newpri = PRI_MIN_IDLE + rtp->prio;
511 sched_class(td, rtp->type); /* XXX fix */
512 sched_user_prio(td, newpri);
514 sched_prio(curthread, td->td_user_pri); /* XXX dubious */
519 pri_to_rtp(struct thread *td, struct rtprio *rtp)
522 mtx_assert(&sched_lock, MA_OWNED);
523 switch (PRI_BASE(td->td_pri_class)) {
525 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
528 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
531 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
536 rtp->type = td->td_pri_class;
539 #if defined(COMPAT_43)
540 #ifndef _SYS_SYSPROTO_H_
541 struct osetrlimit_args {
549 register struct osetrlimit_args *uap;
555 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
557 lim.rlim_cur = olim.rlim_cur;
558 lim.rlim_max = olim.rlim_max;
559 error = kern_setrlimit(td, uap->which, &lim);
563 #ifndef _SYS_SYSPROTO_H_
564 struct ogetrlimit_args {
572 register struct ogetrlimit_args *uap;
579 if (uap->which >= RLIM_NLIMITS)
583 lim_rlimit(p, uap->which, &rl);
587 * XXX would be more correct to convert only RLIM_INFINITY to the
588 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
589 * values. Most 64->32 and 32->16 conversions, including not
590 * unimportant ones of uids are even more broken than what we
591 * do here (they blindly truncate). We don't do this correctly
592 * here since we have little experience with EOVERFLOW yet.
593 * Elsewhere, getuid() can't fail...
595 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
596 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
597 error = copyout(&olim, uap->rlp, sizeof(olim));
600 #endif /* COMPAT_43 */
602 #ifndef _SYS_SYSPROTO_H_
603 struct __setrlimit_args {
611 register struct __setrlimit_args *uap;
616 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
618 error = kern_setrlimit(td, uap->which, &alim);
623 kern_setrlimit(td, which, limp)
628 struct plimit *newlim, *oldlim;
630 register struct rlimit *alimp;
634 if (which >= RLIM_NLIMITS)
638 * Preserve historical bugs by treating negative limits as unsigned.
640 if (limp->rlim_cur < 0)
641 limp->rlim_cur = RLIM_INFINITY;
642 if (limp->rlim_max < 0)
643 limp->rlim_max = RLIM_INFINITY;
647 newlim = lim_alloc();
650 alimp = &oldlim->pl_rlimit[which];
651 if (limp->rlim_cur > alimp->rlim_max ||
652 limp->rlim_max > alimp->rlim_max)
653 if ((error = priv_check_cred(td->td_ucred,
654 PRIV_PROC_SETRLIMIT, SUSER_ALLOWJAIL))) {
659 if (limp->rlim_cur > limp->rlim_max)
660 limp->rlim_cur = limp->rlim_max;
661 lim_copy(newlim, oldlim);
662 alimp = &newlim->pl_rlimit[which];
667 mtx_lock_spin(&sched_lock);
668 p->p_cpulimit = limp->rlim_cur;
669 mtx_unlock_spin(&sched_lock);
672 if (limp->rlim_cur > maxdsiz)
673 limp->rlim_cur = maxdsiz;
674 if (limp->rlim_max > maxdsiz)
675 limp->rlim_max = maxdsiz;
679 if (limp->rlim_cur > maxssiz)
680 limp->rlim_cur = maxssiz;
681 if (limp->rlim_max > maxssiz)
682 limp->rlim_max = maxssiz;
683 oldssiz = alimp->rlim_cur;
687 if (limp->rlim_cur > maxfilesperproc)
688 limp->rlim_cur = maxfilesperproc;
689 if (limp->rlim_max > maxfilesperproc)
690 limp->rlim_max = maxfilesperproc;
694 if (limp->rlim_cur > maxprocperuid)
695 limp->rlim_cur = maxprocperuid;
696 if (limp->rlim_max > maxprocperuid)
697 limp->rlim_max = maxprocperuid;
698 if (limp->rlim_cur < 1)
700 if (limp->rlim_max < 1)
704 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
705 td->td_proc->p_sysent->sv_fixlimit(limp, which);
711 if (which == RLIMIT_STACK) {
713 * Stack is allocated to the max at exec time with only
714 * "rlim_cur" bytes accessible. If stack limit is going
715 * up make more accessible, if going down make inaccessible.
717 if (limp->rlim_cur != oldssiz) {
722 if (limp->rlim_cur > oldssiz) {
723 prot = p->p_sysent->sv_stackprot;
724 size = limp->rlim_cur - oldssiz;
725 addr = p->p_sysent->sv_usrstack -
729 size = oldssiz - limp->rlim_cur;
730 addr = p->p_sysent->sv_usrstack - oldssiz;
732 addr = trunc_page(addr);
733 size = round_page(size);
734 (void)vm_map_protect(&p->p_vmspace->vm_map,
735 addr, addr + size, prot, FALSE);
742 #ifndef _SYS_SYSPROTO_H_
743 struct __getrlimit_args {
752 register struct __getrlimit_args *uap;
758 if (uap->which >= RLIM_NLIMITS)
762 lim_rlimit(p, uap->which, &rlim);
764 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
769 * Transform the running time and tick information for children of proc p
770 * into user and system time usage.
779 PROC_LOCK_ASSERT(p, MA_OWNED);
780 calcru1(p, &p->p_crux, up, sp);
784 * Transform the running time and tick information in proc p into user
785 * and system time usage. If appropriate, include the current time slice
789 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
791 struct rusage_ext rux;
795 PROC_LOCK_ASSERT(p, MA_OWNED);
796 mtx_assert(&sched_lock, MA_NOTOWNED);
797 mtx_lock_spin(&sched_lock);
800 * If we are getting stats for the current process, then add in the
801 * stats that this thread has accumulated in its current time slice.
802 * We reset the thread and CPU state as if we had performed a context
805 if (curthread->td_proc == p) {
808 p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
809 PCPU_SET(switchtime, u);
810 p->p_rux.rux_uticks += td->td_uticks;
812 p->p_rux.rux_iticks += td->td_iticks;
814 p->p_rux.rux_sticks += td->td_sticks;
817 /* Work on a copy of p_rux so we can let go of sched_lock */
819 mtx_unlock_spin(&sched_lock);
820 calcru1(p, &rux, up, sp);
821 /* Update the result from the p_rux copy */
822 p->p_rux.rux_uu = rux.rux_uu;
823 p->p_rux.rux_su = rux.rux_su;
824 p->p_rux.rux_tu = rux.rux_tu;
828 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
831 /* {user, system, interrupt, total} {ticks, usec}: */
832 u_int64_t ut, uu, st, su, it, tt, tu;
834 ut = ruxp->rux_uticks;
835 st = ruxp->rux_sticks;
836 it = ruxp->rux_iticks;
839 /* Avoid divide by zero */
843 tu = cputick2usec(ruxp->rux_runtime);
844 if ((int64_t)tu < 0) {
845 /* XXX: this should be an assert /phk */
846 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
847 (intmax_t)tu, p->p_pid, p->p_comm);
851 if (tu >= ruxp->rux_tu) {
853 * The normal case, time increased.
854 * Enforce monotonicity of bucketed numbers.
857 if (uu < ruxp->rux_uu)
860 if (su < ruxp->rux_su)
862 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
864 * When we calibrate the cputicker, it is not uncommon to
865 * see the presumably fixed frequency increase slightly over
866 * time as a result of thermal stabilization and NTP
867 * discipline (of the reference clock). We therefore ignore
868 * a bit of backwards slop because we expect to catch up
869 * shortly. We use a 3 microsecond limit to catch low
870 * counts and a 1% limit for high counts.
875 } else { /* tu < ruxp->rux_tu */
877 * What happene here was likely that a laptop, which ran at
878 * a reduced clock frequency at boot, kicked into high gear.
879 * The wisdom of spamming this message in that case is
880 * dubious, but it might also be indicative of something
881 * serious, so lets keep it and hope laptops can be made
882 * more truthful about their CPU speed via ACPI.
884 printf("calcru: runtime went backwards from %ju usec "
885 "to %ju usec for pid %d (%s)\n",
886 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
887 p->p_pid, p->p_comm);
896 up->tv_sec = uu / 1000000;
897 up->tv_usec = uu % 1000000;
898 sp->tv_sec = su / 1000000;
899 sp->tv_usec = su % 1000000;
902 #ifndef _SYS_SYSPROTO_H_
903 struct getrusage_args {
905 struct rusage *rusage;
910 register struct thread *td;
911 register struct getrusage_args *uap;
916 error = kern_getrusage(td, uap->who, &ru);
918 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
923 kern_getrusage(td, who, rup)
935 *rup = p->p_stats->p_ru;
936 calcru(p, &rup->ru_utime, &rup->ru_stime);
939 case RUSAGE_CHILDREN:
940 *rup = p->p_stats->p_cru;
941 calccru(p, &rup->ru_utime, &rup->ru_stime);
953 ruadd(ru, rux, ru2, rux2)
955 struct rusage_ext *rux;
957 struct rusage_ext *rux2;
959 register long *ip, *ip2;
962 rux->rux_runtime += rux2->rux_runtime;
963 rux->rux_uticks += rux2->rux_uticks;
964 rux->rux_sticks += rux2->rux_sticks;
965 rux->rux_iticks += rux2->rux_iticks;
966 rux->rux_uu += rux2->rux_uu;
967 rux->rux_su += rux2->rux_su;
968 rux->rux_tu += rux2->rux_tu;
969 if (ru->ru_maxrss < ru2->ru_maxrss)
970 ru->ru_maxrss = ru2->ru_maxrss;
972 ip2 = &ru2->ru_first;
973 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
978 * Allocate a new resource limits structure and initialize its
979 * reference count and mutex pointer.
986 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
987 refcount_init(&limp->pl_refcnt, 1);
996 refcount_acquire(&limp->pl_refcnt);
1002 struct plimit *limp;
1005 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1006 if (refcount_release(&limp->pl_refcnt))
1007 free((void *)limp, M_PLIMIT);
1011 * Make a copy of the plimit structure.
1012 * We share these structures copy-on-write after fork.
1016 struct plimit *dst, *src;
1019 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1020 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1024 * Return the hard limit for a particular system resource. The
1025 * which parameter specifies the index into the rlimit array.
1028 lim_max(struct proc *p, int which)
1032 lim_rlimit(p, which, &rl);
1033 return (rl.rlim_max);
1037 * Return the current (soft) limit for a particular system resource.
1038 * The which parameter which specifies the index into the rlimit array
1041 lim_cur(struct proc *p, int which)
1045 lim_rlimit(p, which, &rl);
1046 return (rl.rlim_cur);
1050 * Return a copy of the entire rlimit structure for the system limit
1051 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1054 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1057 PROC_LOCK_ASSERT(p, MA_OWNED);
1058 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1059 ("request for invalid resource limit"));
1060 *rlp = p->p_limit->pl_rlimit[which];
1061 if (p->p_sysent->sv_fixlimit != NULL)
1062 p->p_sysent->sv_fixlimit(rlp, which);
1066 * Find the uidinfo structure for a uid. This structure is used to
1067 * track the total resource consumption (process count, socket buffer
1068 * size, etc.) for the uid and impose limits.
1074 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1075 mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF);
1079 * Look up a uidinfo struct for the parameter uid.
1080 * uihashtbl_mtx must be locked.
1082 static struct uidinfo *
1086 struct uihashhead *uipp;
1087 struct uidinfo *uip;
1089 mtx_assert(&uihashtbl_mtx, MA_OWNED);
1091 LIST_FOREACH(uip, uipp, ui_hash)
1092 if (uip->ui_uid == uid)
1099 * Find or allocate a struct uidinfo for a particular uid.
1100 * Increase refcount on uidinfo struct returned.
1101 * uifree() should be called on a struct uidinfo when released.
1107 struct uidinfo *old_uip, *uip;
1109 mtx_lock(&uihashtbl_mtx);
1110 uip = uilookup(uid);
1112 mtx_unlock(&uihashtbl_mtx);
1113 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1114 mtx_lock(&uihashtbl_mtx);
1116 * There's a chance someone created our uidinfo while we
1117 * were in malloc and not holding the lock, so we have to
1118 * make sure we don't insert a duplicate uidinfo.
1120 if ((old_uip = uilookup(uid)) != NULL) {
1121 /* Someone else beat us to it. */
1122 free(uip, M_UIDINFO);
1125 uip->ui_mtxp = mtx_pool_alloc(mtxpool_sleep);
1127 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1131 mtx_unlock(&uihashtbl_mtx);
1136 * Place another refcount on a uidinfo struct.
1140 struct uidinfo *uip;
1145 UIDINFO_UNLOCK(uip);
1149 * Since uidinfo structs have a long lifetime, we use an
1150 * opportunistic refcounting scheme to avoid locking the lookup hash
1153 * If the refcount hits 0, we need to free the structure,
1154 * which means we need to lock the hash.
1156 * After locking the struct and lowering the refcount, if we find
1157 * that we don't need to free, simply unlock and return.
1159 * If refcount lowering results in need to free, bump the count
1160 * back up, lose the lock and aquire the locks in the proper
1161 * order to try again.
1165 struct uidinfo *uip;
1168 /* Prepare for optimal case. */
1171 if (--uip->ui_ref != 0) {
1172 UIDINFO_UNLOCK(uip);
1176 /* Prepare for suboptimal case. */
1178 UIDINFO_UNLOCK(uip);
1179 mtx_lock(&uihashtbl_mtx);
1183 * We must subtract one from the count again because we backed out
1184 * our initial subtraction before dropping the lock.
1185 * Since another thread may have added a reference after we dropped the
1186 * initial lock we have to test for zero again.
1188 if (--uip->ui_ref == 0) {
1189 LIST_REMOVE(uip, ui_hash);
1190 mtx_unlock(&uihashtbl_mtx);
1191 if (uip->ui_sbsize != 0)
1192 printf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1193 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1194 if (uip->ui_proccnt != 0)
1195 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1196 uip->ui_uid, uip->ui_proccnt);
1197 UIDINFO_UNLOCK(uip);
1198 FREE(uip, M_UIDINFO);
1202 mtx_unlock(&uihashtbl_mtx);
1203 UIDINFO_UNLOCK(uip);
1207 * Change the count associated with number of processes
1208 * a given user is using. When 'max' is 0, don't enforce a limit
1211 chgproccnt(uip, diff, max)
1212 struct uidinfo *uip;
1218 /* Don't allow them to exceed max, but allow subtraction. */
1219 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1220 UIDINFO_UNLOCK(uip);
1223 uip->ui_proccnt += diff;
1224 if (uip->ui_proccnt < 0)
1225 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1226 UIDINFO_UNLOCK(uip);
1231 * Change the total socket buffer size a user has used.
1234 chgsbsize(uip, hiwat, to, max)
1235 struct uidinfo *uip;
1243 new = uip->ui_sbsize + to - *hiwat;
1244 /* Don't allow them to exceed max, but allow subtraction. */
1245 if (to > *hiwat && new > max) {
1246 UIDINFO_UNLOCK(uip);
1249 uip->ui_sbsize = new;
1250 UIDINFO_UNLOCK(uip);
1253 printf("negative sbsize for uid = %d\n", uip->ui_uid);