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/rwlock.h>
55 #include <sys/sched.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysent.h>
63 #include <vm/vm_param.h>
65 #include <vm/vm_map.h>
68 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
69 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
70 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
71 static struct rwlock uihashtbl_lock;
72 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
73 static u_long uihash; /* size of hash table - 1 */
75 static void calcru1(struct proc *p, struct rusage_ext *ruxp,
76 struct timeval *up, struct timeval *sp);
77 static int donice(struct thread *td, struct proc *chgp, int n);
78 static struct uidinfo *uilookup(uid_t uid);
79 static void ruxagg_locked(struct rusage_ext *rux, struct thread *td);
82 * Resource controls and accounting.
84 #ifndef _SYS_SYSPROTO_H_
85 struct getpriority_args {
93 register struct getpriority_args *uap;
101 switch (uap->which) {
105 low = td->td_proc->p_nice;
110 if (p_cansee(td, p) == 0)
117 sx_slock(&proctree_lock);
119 pg = td->td_proc->p_pgrp;
122 pg = pgfind(uap->who);
124 sx_sunlock(&proctree_lock);
128 sx_sunlock(&proctree_lock);
129 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
131 if (p->p_state == PRS_NORMAL &&
132 p_cansee(td, p) == 0) {
143 uap->who = td->td_ucred->cr_uid;
144 sx_slock(&allproc_lock);
145 FOREACH_PROC_IN_SYSTEM(p) {
147 if (p->p_state == PRS_NORMAL &&
148 p_cansee(td, p) == 0 &&
149 p->p_ucred->cr_uid == uap->who) {
155 sx_sunlock(&allproc_lock);
162 if (low == PRIO_MAX + 1 && error == 0)
164 td->td_retval[0] = low;
168 #ifndef _SYS_SYSPROTO_H_
169 struct setpriority_args {
178 struct setpriority_args *uap;
180 struct proc *curp, *p;
182 int found = 0, error = 0;
185 switch (uap->which) {
189 error = donice(td, curp, uap->prio);
195 error = p_cansee(td, p);
197 error = donice(td, p, uap->prio);
204 sx_slock(&proctree_lock);
209 pg = pgfind(uap->who);
211 sx_sunlock(&proctree_lock);
215 sx_sunlock(&proctree_lock);
216 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
218 if (p->p_state == PRS_NORMAL &&
219 p_cansee(td, p) == 0) {
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_state == PRS_NORMAL &&
235 p->p_ucred->cr_uid == uap->who &&
236 p_cansee(td, p) == 0) {
237 error = donice(td, p, uap->prio);
242 sx_sunlock(&allproc_lock);
249 if (found == 0 && error == 0)
255 * Set "nice" for a (whole) process.
258 donice(struct thread *td, struct proc *p, int n)
262 PROC_LOCK_ASSERT(p, MA_OWNED);
263 if ((error = p_cansched(td, p)))
269 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
276 * Set realtime priority for LWP.
278 #ifndef _SYS_SYSPROTO_H_
279 struct rtprio_thread_args {
286 rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
293 /* Perform copyin before acquiring locks if needed. */
294 if (uap->function == RTP_SET)
295 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
300 * Though lwpid is unique, only current process is supported
301 * since there is no efficient way to look up a LWP yet.
306 switch (uap->function) {
308 if ((error = p_cansee(td, p)))
310 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
313 td1 = thread_find(p, uap->lwpid);
315 pri_to_rtp(td1, &rtp);
319 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
321 if ((error = p_cansched(td, p)) || (error = cierror))
324 /* Disallow setting rtprio in most cases if not superuser. */
326 * Realtime priority has to be restricted for reasons which should be
327 * obvious. However, for idle priority, there is a potential for
328 * system deadlock if an idleprio process gains a lock on a resource
329 * that other processes need (and the idleprio process can't run
330 * due to a CPU-bound normal process). Fix me! XXX
333 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
335 if (rtp.type != RTP_PRIO_NORMAL) {
337 error = priv_check(td, PRIV_SCHED_RTPRIO);
342 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
345 td1 = thread_find(p, uap->lwpid);
347 error = rtp_to_pri(&rtp, td1);
360 * Set realtime priority.
362 #ifndef _SYS_SYSPROTO_H_
371 struct thread *td; /* curthread */
372 register struct rtprio_args *uap;
379 /* Perform copyin before acquiring locks if needed. */
380 if (uap->function == RTP_SET)
381 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
394 switch (uap->function) {
396 if ((error = p_cansee(td, p)))
399 * Return OUR priority if no pid specified,
400 * or if one is, report the highest priority
401 * in the process. There isn't much more you can do as
402 * there is only room to return a single priority.
403 * Note: specifying our own pid is not the same
404 * as leaving it zero.
407 pri_to_rtp(td, &rtp);
411 rtp.type = RTP_PRIO_IDLE;
412 rtp.prio = RTP_PRIO_MAX;
413 FOREACH_THREAD_IN_PROC(p, tdp) {
414 pri_to_rtp(tdp, &rtp2);
415 if (rtp2.type < rtp.type ||
416 (rtp2.type == rtp.type &&
417 rtp2.prio < rtp.prio)) {
418 rtp.type = rtp2.type;
419 rtp.prio = rtp2.prio;
424 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
426 if ((error = p_cansched(td, p)) || (error = cierror))
429 /* Disallow setting rtprio in most cases if not superuser. */
431 * Realtime priority has to be restricted for reasons which should be
432 * obvious. However, for idle priority, there is a potential for
433 * system deadlock if an idleprio process gains a lock on a resource
434 * that other processes need (and the idleprio process can't run
435 * due to a CPU-bound normal process). Fix me! XXX
438 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
440 if (rtp.type != RTP_PRIO_NORMAL) {
442 error = priv_check(td, PRIV_SCHED_RTPRIO);
448 * If we are setting our own priority, set just our
449 * thread but if we are doing another process,
450 * do all the threads on that process. If we
451 * specify our own pid we do the latter.
454 error = rtp_to_pri(&rtp, td);
456 FOREACH_THREAD_IN_PROC(p, td) {
457 if ((error = rtp_to_pri(&rtp, td)) != 0)
471 rtp_to_pri(struct rtprio *rtp, struct thread *td)
476 switch (RTP_PRIO_BASE(rtp->type)) {
477 case RTP_PRIO_REALTIME:
478 if (rtp->prio > RTP_PRIO_MAX)
480 newpri = PRI_MIN_REALTIME + rtp->prio;
482 case RTP_PRIO_NORMAL:
483 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
485 newpri = PRI_MIN_TIMESHARE + rtp->prio;
488 if (rtp->prio > RTP_PRIO_MAX)
490 newpri = PRI_MIN_IDLE + rtp->prio;
497 sched_class(td, rtp->type); /* XXX fix */
498 oldpri = td->td_user_pri;
499 sched_user_prio(td, newpri);
500 if (td->td_user_pri != oldpri && (td == curthread ||
501 td->td_priority == oldpri || td->td_user_pri >= PRI_MAX_REALTIME))
502 sched_prio(td, td->td_user_pri);
503 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
505 umtx_pi_adjust(td, oldpri);
512 pri_to_rtp(struct thread *td, struct rtprio *rtp)
516 switch (PRI_BASE(td->td_pri_class)) {
518 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
521 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
524 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
529 rtp->type = td->td_pri_class;
533 #if defined(COMPAT_43)
534 #ifndef _SYS_SYSPROTO_H_
535 struct osetrlimit_args {
543 register struct osetrlimit_args *uap;
549 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
551 lim.rlim_cur = olim.rlim_cur;
552 lim.rlim_max = olim.rlim_max;
553 error = kern_setrlimit(td, uap->which, &lim);
557 #ifndef _SYS_SYSPROTO_H_
558 struct ogetrlimit_args {
566 register struct ogetrlimit_args *uap;
573 if (uap->which >= RLIM_NLIMITS)
577 lim_rlimit(p, uap->which, &rl);
581 * XXX would be more correct to convert only RLIM_INFINITY to the
582 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
583 * values. Most 64->32 and 32->16 conversions, including not
584 * unimportant ones of uids are even more broken than what we
585 * do here (they blindly truncate). We don't do this correctly
586 * here since we have little experience with EOVERFLOW yet.
587 * Elsewhere, getuid() can't fail...
589 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
590 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
591 error = copyout(&olim, uap->rlp, sizeof(olim));
594 #endif /* COMPAT_43 */
596 #ifndef _SYS_SYSPROTO_H_
597 struct __setrlimit_args {
605 register struct __setrlimit_args *uap;
610 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
612 error = kern_setrlimit(td, uap->which, &alim);
624 PROC_LOCK_ASSERT(p, MA_OWNED);
626 * Check if the process exceeds its cpu resource allocation. If
627 * it reaches the max, arrange to kill the process in ast().
629 if (p->p_cpulimit == RLIM_INFINITY)
632 FOREACH_THREAD_IN_PROC(p, td) {
636 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
637 lim_rlimit(p, RLIMIT_CPU, &rlim);
638 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
639 killproc(p, "exceeded maximum CPU limit");
641 if (p->p_cpulimit < rlim.rlim_max)
646 if ((p->p_flag & P_WEXIT) == 0)
647 callout_reset(&p->p_limco, hz, lim_cb, p);
651 kern_setrlimit(td, which, limp)
656 struct plimit *newlim, *oldlim;
658 register struct rlimit *alimp;
659 struct rlimit oldssiz;
662 if (which >= RLIM_NLIMITS)
666 * Preserve historical bugs by treating negative limits as unsigned.
668 if (limp->rlim_cur < 0)
669 limp->rlim_cur = RLIM_INFINITY;
670 if (limp->rlim_max < 0)
671 limp->rlim_max = RLIM_INFINITY;
673 oldssiz.rlim_cur = 0;
675 newlim = lim_alloc();
678 alimp = &oldlim->pl_rlimit[which];
679 if (limp->rlim_cur > alimp->rlim_max ||
680 limp->rlim_max > alimp->rlim_max)
681 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
686 if (limp->rlim_cur > limp->rlim_max)
687 limp->rlim_cur = limp->rlim_max;
688 lim_copy(newlim, oldlim);
689 alimp = &newlim->pl_rlimit[which];
694 if (limp->rlim_cur != RLIM_INFINITY &&
695 p->p_cpulimit == RLIM_INFINITY)
696 callout_reset(&p->p_limco, hz, lim_cb, p);
697 p->p_cpulimit = limp->rlim_cur;
700 if (limp->rlim_cur > maxdsiz)
701 limp->rlim_cur = maxdsiz;
702 if (limp->rlim_max > maxdsiz)
703 limp->rlim_max = maxdsiz;
707 if (limp->rlim_cur > maxssiz)
708 limp->rlim_cur = maxssiz;
709 if (limp->rlim_max > maxssiz)
710 limp->rlim_max = maxssiz;
712 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
713 td->td_proc->p_sysent->sv_fixlimit(&oldssiz,
718 if (limp->rlim_cur > maxfilesperproc)
719 limp->rlim_cur = maxfilesperproc;
720 if (limp->rlim_max > maxfilesperproc)
721 limp->rlim_max = maxfilesperproc;
725 if (limp->rlim_cur > maxprocperuid)
726 limp->rlim_cur = maxprocperuid;
727 if (limp->rlim_max > maxprocperuid)
728 limp->rlim_max = maxprocperuid;
729 if (limp->rlim_cur < 1)
731 if (limp->rlim_max < 1)
735 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
736 td->td_proc->p_sysent->sv_fixlimit(limp, which);
742 if (which == RLIMIT_STACK) {
744 * Stack is allocated to the max at exec time with only
745 * "rlim_cur" bytes accessible. If stack limit is going
746 * up make more accessible, if going down make inaccessible.
748 if (limp->rlim_cur != oldssiz.rlim_cur) {
753 if (limp->rlim_cur > oldssiz.rlim_cur) {
754 prot = p->p_sysent->sv_stackprot;
755 size = limp->rlim_cur - oldssiz.rlim_cur;
756 addr = p->p_sysent->sv_usrstack -
760 size = oldssiz.rlim_cur - limp->rlim_cur;
761 addr = p->p_sysent->sv_usrstack -
764 addr = trunc_page(addr);
765 size = round_page(size);
766 (void)vm_map_protect(&p->p_vmspace->vm_map,
767 addr, addr + size, prot, FALSE);
774 #ifndef _SYS_SYSPROTO_H_
775 struct __getrlimit_args {
784 register struct __getrlimit_args *uap;
790 if (uap->which >= RLIM_NLIMITS)
794 lim_rlimit(p, uap->which, &rlim);
796 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
801 * Transform the running time and tick information for children of proc p
802 * into user and system time usage.
811 PROC_LOCK_ASSERT(p, MA_OWNED);
812 calcru1(p, &p->p_crux, up, sp);
816 * Transform the running time and tick information in proc p into user
817 * and system time usage. If appropriate, include the current time slice
821 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
826 PROC_LOCK_ASSERT(p, MA_OWNED);
827 PROC_SLOCK_ASSERT(p, MA_OWNED);
829 * If we are getting stats for the current process, then add in the
830 * stats that this thread has accumulated in its current time slice.
831 * We reset the thread and CPU state as if we had performed a context
835 if (td->td_proc == p) {
837 p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
838 PCPU_SET(switchtime, u);
840 /* Make sure the per-thread stats are current. */
841 FOREACH_THREAD_IN_PROC(p, td) {
842 if (td->td_incruntime == 0)
846 calcru1(p, &p->p_rux, up, sp);
850 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
853 /* {user, system, interrupt, total} {ticks, usec}: */
854 u_int64_t ut, uu, st, su, it, tt, tu;
856 ut = ruxp->rux_uticks;
857 st = ruxp->rux_sticks;
858 it = ruxp->rux_iticks;
861 /* Avoid divide by zero */
865 tu = cputick2usec(ruxp->rux_runtime);
866 if ((int64_t)tu < 0) {
867 /* XXX: this should be an assert /phk */
868 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
869 (intmax_t)tu, p->p_pid, p->p_comm);
873 if (tu >= ruxp->rux_tu) {
875 * The normal case, time increased.
876 * Enforce monotonicity of bucketed numbers.
879 if (uu < ruxp->rux_uu)
882 if (su < ruxp->rux_su)
884 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
886 * When we calibrate the cputicker, it is not uncommon to
887 * see the presumably fixed frequency increase slightly over
888 * time as a result of thermal stabilization and NTP
889 * discipline (of the reference clock). We therefore ignore
890 * a bit of backwards slop because we expect to catch up
891 * shortly. We use a 3 microsecond limit to catch low
892 * counts and a 1% limit for high counts.
897 } else { /* tu < ruxp->rux_tu */
899 * What happened here was likely that a laptop, which ran at
900 * a reduced clock frequency at boot, kicked into high gear.
901 * The wisdom of spamming this message in that case is
902 * dubious, but it might also be indicative of something
903 * serious, so lets keep it and hope laptops can be made
904 * more truthful about their CPU speed via ACPI.
906 printf("calcru: runtime went backwards from %ju usec "
907 "to %ju usec for pid %d (%s)\n",
908 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
909 p->p_pid, p->p_comm);
918 up->tv_sec = uu / 1000000;
919 up->tv_usec = uu % 1000000;
920 sp->tv_sec = su / 1000000;
921 sp->tv_usec = su % 1000000;
924 #ifndef _SYS_SYSPROTO_H_
925 struct getrusage_args {
927 struct rusage *rusage;
932 register struct thread *td;
933 register struct getrusage_args *uap;
938 error = kern_getrusage(td, uap->who, &ru);
940 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
945 kern_getrusage(struct thread *td, int who, struct rusage *rup)
955 rufetchcalc(p, rup, &rup->ru_utime,
959 case RUSAGE_CHILDREN:
960 *rup = p->p_stats->p_cru;
961 calccru(p, &rup->ru_utime, &rup->ru_stime);
970 calcru1(p, &td->td_rux, &rup->ru_utime, &rup->ru_stime);
982 rucollect(struct rusage *ru, struct rusage *ru2)
987 if (ru->ru_maxrss < ru2->ru_maxrss)
988 ru->ru_maxrss = ru2->ru_maxrss;
990 ip2 = &ru2->ru_first;
991 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
996 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
997 struct rusage_ext *rux2)
1000 rux->rux_runtime += rux2->rux_runtime;
1001 rux->rux_uticks += rux2->rux_uticks;
1002 rux->rux_sticks += rux2->rux_sticks;
1003 rux->rux_iticks += rux2->rux_iticks;
1004 rux->rux_uu += rux2->rux_uu;
1005 rux->rux_su += rux2->rux_su;
1006 rux->rux_tu += rux2->rux_tu;
1011 * Aggregate tick counts into the proc's rusage_ext.
1014 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1017 THREAD_LOCK_ASSERT(td, MA_OWNED);
1018 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1019 rux->rux_runtime += td->td_incruntime;
1020 rux->rux_uticks += td->td_uticks;
1021 rux->rux_sticks += td->td_sticks;
1022 rux->rux_iticks += td->td_iticks;
1026 ruxagg(struct proc *p, struct thread *td)
1030 ruxagg_locked(&p->p_rux, td);
1031 ruxagg_locked(&td->td_rux, td);
1032 td->td_incruntime = 0;
1040 * Update the rusage_ext structure and fetch a valid aggregate rusage
1041 * for proc p if storage for one is supplied.
1044 rufetch(struct proc *p, struct rusage *ru)
1048 PROC_SLOCK_ASSERT(p, MA_OWNED);
1051 if (p->p_numthreads > 0) {
1052 FOREACH_THREAD_IN_PROC(p, td) {
1054 rucollect(ru, &td->td_ru);
1060 * Atomically perform a rufetch and a calcru together.
1061 * Consumers, can safely assume the calcru is executed only once
1062 * rufetch is completed.
1065 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1076 * Allocate a new resource limits structure and initialize its
1077 * reference count and mutex pointer.
1082 struct plimit *limp;
1084 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1085 refcount_init(&limp->pl_refcnt, 1);
1091 struct plimit *limp;
1094 refcount_acquire(&limp->pl_refcnt);
1099 lim_fork(struct proc *p1, struct proc *p2)
1101 p2->p_limit = lim_hold(p1->p_limit);
1102 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1103 if (p1->p_cpulimit != RLIM_INFINITY)
1104 callout_reset(&p2->p_limco, hz, lim_cb, p2);
1109 struct plimit *limp;
1112 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1113 if (refcount_release(&limp->pl_refcnt))
1114 free((void *)limp, M_PLIMIT);
1118 * Make a copy of the plimit structure.
1119 * We share these structures copy-on-write after fork.
1123 struct plimit *dst, *src;
1126 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1127 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1131 * Return the hard limit for a particular system resource. The
1132 * which parameter specifies the index into the rlimit array.
1135 lim_max(struct proc *p, int which)
1139 lim_rlimit(p, which, &rl);
1140 return (rl.rlim_max);
1144 * Return the current (soft) limit for a particular system resource.
1145 * The which parameter which specifies the index into the rlimit array
1148 lim_cur(struct proc *p, int which)
1152 lim_rlimit(p, which, &rl);
1153 return (rl.rlim_cur);
1157 * Return a copy of the entire rlimit structure for the system limit
1158 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1161 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1164 PROC_LOCK_ASSERT(p, MA_OWNED);
1165 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1166 ("request for invalid resource limit"));
1167 *rlp = p->p_limit->pl_rlimit[which];
1168 if (p->p_sysent->sv_fixlimit != NULL)
1169 p->p_sysent->sv_fixlimit(rlp, which);
1173 * Find the uidinfo structure for a uid. This structure is used to
1174 * track the total resource consumption (process count, socket buffer
1175 * size, etc.) for the uid and impose limits.
1181 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1182 rw_init(&uihashtbl_lock, "uidinfo hash");
1186 * Look up a uidinfo struct for the parameter uid.
1187 * uihashtbl_lock must be locked.
1189 static struct uidinfo *
1193 struct uihashhead *uipp;
1194 struct uidinfo *uip;
1196 rw_assert(&uihashtbl_lock, RA_LOCKED);
1198 LIST_FOREACH(uip, uipp, ui_hash)
1199 if (uip->ui_uid == uid)
1206 * Find or allocate a struct uidinfo for a particular uid.
1207 * Increase refcount on uidinfo struct returned.
1208 * uifree() should be called on a struct uidinfo when released.
1214 struct uidinfo *old_uip, *uip;
1216 rw_rlock(&uihashtbl_lock);
1217 uip = uilookup(uid);
1219 rw_runlock(&uihashtbl_lock);
1220 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1221 rw_wlock(&uihashtbl_lock);
1223 * There's a chance someone created our uidinfo while we
1224 * were in malloc and not holding the lock, so we have to
1225 * make sure we don't insert a duplicate uidinfo.
1227 if ((old_uip = uilookup(uid)) != NULL) {
1228 /* Someone else beat us to it. */
1229 free(uip, M_UIDINFO);
1232 refcount_init(&uip->ui_ref, 0);
1234 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1236 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1240 rw_unlock(&uihashtbl_lock);
1245 * Place another refcount on a uidinfo struct.
1249 struct uidinfo *uip;
1252 refcount_acquire(&uip->ui_ref);
1256 * Since uidinfo structs have a long lifetime, we use an
1257 * opportunistic refcounting scheme to avoid locking the lookup hash
1260 * If the refcount hits 0, we need to free the structure,
1261 * which means we need to lock the hash.
1263 * After locking the struct and lowering the refcount, if we find
1264 * that we don't need to free, simply unlock and return.
1266 * If refcount lowering results in need to free, bump the count
1267 * back up, lose the lock and acquire the locks in the proper
1268 * order to try again.
1272 struct uidinfo *uip;
1276 /* Prepare for optimal case. */
1278 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1281 /* Prepare for suboptimal case. */
1282 rw_wlock(&uihashtbl_lock);
1283 if (refcount_release(&uip->ui_ref)) {
1284 LIST_REMOVE(uip, ui_hash);
1285 rw_wunlock(&uihashtbl_lock);
1286 if (uip->ui_sbsize != 0)
1287 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1288 uip->ui_uid, uip->ui_sbsize);
1289 if (uip->ui_proccnt != 0)
1290 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1291 uip->ui_uid, uip->ui_proccnt);
1292 if (uip->ui_vmsize != 0)
1293 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1294 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1295 mtx_destroy(&uip->ui_vmsize_mtx);
1296 free(uip, M_UIDINFO);
1300 * Someone added a reference between atomic_cmpset_int() and
1301 * rw_wlock(&uihashtbl_lock).
1303 rw_wunlock(&uihashtbl_lock);
1307 * Change the count associated with number of processes
1308 * a given user is using. When 'max' is 0, don't enforce a limit
1311 chgproccnt(uip, diff, max)
1312 struct uidinfo *uip;
1317 /* Don't allow them to exceed max, but allow subtraction. */
1318 if (diff > 0 && max != 0) {
1319 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1320 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1324 atomic_add_long(&uip->ui_proccnt, (long)diff);
1325 if (uip->ui_proccnt < 0)
1326 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1332 * Change the total socket buffer size a user has used.
1335 chgsbsize(uip, hiwat, to, max)
1336 struct uidinfo *uip;
1345 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1346 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1350 atomic_add_long(&uip->ui_sbsize, (long)diff);
1351 if (uip->ui_sbsize < 0)
1352 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1359 * Change the count associated with number of pseudo-terminals
1360 * a given user is using. When 'max' is 0, don't enforce a limit
1363 chgptscnt(uip, diff, max)
1364 struct uidinfo *uip;
1369 /* Don't allow them to exceed max, but allow subtraction. */
1370 if (diff > 0 && max != 0) {
1371 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1372 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1376 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1377 if (uip->ui_ptscnt < 0)
1378 printf("negative ptscnt for uid = %d\n", uip->ui_uid);