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/racct.h>
54 #include <sys/resourcevar.h>
55 #include <sys/rwlock.h>
56 #include <sys/sched.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysent.h>
64 #include <vm/vm_param.h>
66 #include <vm/vm_map.h>
69 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
70 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
71 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
72 static struct rwlock uihashtbl_lock;
73 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
74 static u_long uihash; /* size of hash table - 1 */
76 static void calcru1(struct proc *p, struct rusage_ext *ruxp,
77 struct timeval *up, struct timeval *sp);
78 static int donice(struct thread *td, struct proc *chgp, int n);
79 static struct uidinfo *uilookup(uid_t uid);
80 static void ruxagg_locked(struct rusage_ext *rux, struct thread *td);
83 * Resource controls and accounting.
85 #ifndef _SYS_SYSPROTO_H_
86 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->p_state == PRS_NORMAL &&
133 p_cansee(td, p) == 0) {
144 uap->who = td->td_ucred->cr_uid;
145 sx_slock(&allproc_lock);
146 FOREACH_PROC_IN_SYSTEM(p) {
148 if (p->p_state == PRS_NORMAL &&
149 p_cansee(td, p) == 0 &&
150 p->p_ucred->cr_uid == uap->who) {
156 sx_sunlock(&allproc_lock);
163 if (low == PRIO_MAX + 1 && error == 0)
165 td->td_retval[0] = low;
169 #ifndef _SYS_SYSPROTO_H_
170 struct setpriority_args {
179 struct setpriority_args *uap;
181 struct proc *curp, *p;
183 int found = 0, error = 0;
186 switch (uap->which) {
190 error = donice(td, curp, uap->prio);
196 error = p_cansee(td, p);
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->p_state == PRS_NORMAL &&
220 p_cansee(td, p) == 0) {
221 error = donice(td, p, uap->prio);
231 uap->who = td->td_ucred->cr_uid;
232 sx_slock(&allproc_lock);
233 FOREACH_PROC_IN_SYSTEM(p) {
235 if (p->p_state == PRS_NORMAL &&
236 p->p_ucred->cr_uid == uap->who &&
237 p_cansee(td, p) == 0) {
238 error = donice(td, p, uap->prio);
243 sx_sunlock(&allproc_lock);
250 if (found == 0 && error == 0)
256 * Set "nice" for a (whole) process.
259 donice(struct thread *td, struct proc *p, int n)
263 PROC_LOCK_ASSERT(p, MA_OWNED);
264 if ((error = p_cansched(td, p)))
270 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
277 * Set realtime priority for LWP.
279 #ifndef _SYS_SYSPROTO_H_
280 struct rtprio_thread_args {
287 rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
294 /* Perform copyin before acquiring locks if needed. */
295 if (uap->function == RTP_SET)
296 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
300 if (uap->lwpid == 0 || uap->lwpid == td->td_tid) {
305 /* Only look up thread in current process */
306 td1 = tdfind(uap->lwpid, curproc->p_pid);
312 switch (uap->function) {
314 if ((error = p_cansee(td, p)))
316 pri_to_rtp(td1, &rtp);
318 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
320 if ((error = p_cansched(td, p)) || (error = cierror))
323 /* Disallow setting rtprio in most cases if not superuser. */
325 * Realtime priority has to be restricted for reasons which should be
326 * obvious. However, for idle priority, there is a potential for
327 * system deadlock if an idleprio process gains a lock on a resource
328 * that other processes need (and the idleprio process can't run
329 * due to a CPU-bound normal process). Fix me! XXX
332 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
334 if (rtp.type != RTP_PRIO_NORMAL) {
336 error = priv_check(td, PRIV_SCHED_RTPRIO);
340 error = rtp_to_pri(&rtp, td1);
351 * Set realtime priority.
353 #ifndef _SYS_SYSPROTO_H_
362 struct thread *td; /* curthread */
363 register struct rtprio_args *uap;
370 /* Perform copyin before acquiring locks if needed. */
371 if (uap->function == RTP_SET)
372 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
385 switch (uap->function) {
387 if ((error = p_cansee(td, p)))
390 * Return OUR priority if no pid specified,
391 * or if one is, report the highest priority
392 * in the process. There isn't much more you can do as
393 * there is only room to return a single priority.
394 * Note: specifying our own pid is not the same
395 * as leaving it zero.
398 pri_to_rtp(td, &rtp);
402 rtp.type = RTP_PRIO_IDLE;
403 rtp.prio = RTP_PRIO_MAX;
404 FOREACH_THREAD_IN_PROC(p, tdp) {
405 pri_to_rtp(tdp, &rtp2);
406 if (rtp2.type < rtp.type ||
407 (rtp2.type == rtp.type &&
408 rtp2.prio < rtp.prio)) {
409 rtp.type = rtp2.type;
410 rtp.prio = rtp2.prio;
415 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
417 if ((error = p_cansched(td, p)) || (error = cierror))
420 /* Disallow setting rtprio in most cases if not superuser. */
422 * Realtime priority has to be restricted for reasons which should be
423 * obvious. However, for idle priority, there is a potential for
424 * system deadlock if an idleprio process gains a lock on a resource
425 * that other processes need (and the idleprio process can't run
426 * due to a CPU-bound normal process). Fix me! XXX
429 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
431 if (rtp.type != RTP_PRIO_NORMAL) {
433 error = priv_check(td, PRIV_SCHED_RTPRIO);
439 * If we are setting our own priority, set just our
440 * thread but if we are doing another process,
441 * do all the threads on that process. If we
442 * specify our own pid we do the latter.
445 error = rtp_to_pri(&rtp, td);
447 FOREACH_THREAD_IN_PROC(p, td) {
448 if ((error = rtp_to_pri(&rtp, td)) != 0)
462 rtp_to_pri(struct rtprio *rtp, struct thread *td)
467 switch (RTP_PRIO_BASE(rtp->type)) {
468 case RTP_PRIO_REALTIME:
469 if (rtp->prio > RTP_PRIO_MAX)
471 newpri = PRI_MIN_REALTIME + rtp->prio;
473 case RTP_PRIO_NORMAL:
474 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
476 newpri = PRI_MIN_TIMESHARE + rtp->prio;
479 if (rtp->prio > RTP_PRIO_MAX)
481 newpri = PRI_MIN_IDLE + rtp->prio;
488 sched_class(td, rtp->type); /* XXX fix */
489 oldpri = td->td_user_pri;
490 sched_user_prio(td, newpri);
492 sched_prio(curthread, td->td_user_pri); /* XXX dubious */
493 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
496 umtx_pi_adjust(td, oldpri);
504 pri_to_rtp(struct thread *td, struct rtprio *rtp)
508 switch (PRI_BASE(td->td_pri_class)) {
510 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
513 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
516 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
521 rtp->type = td->td_pri_class;
525 #if defined(COMPAT_43)
526 #ifndef _SYS_SYSPROTO_H_
527 struct osetrlimit_args {
535 register struct osetrlimit_args *uap;
541 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
543 lim.rlim_cur = olim.rlim_cur;
544 lim.rlim_max = olim.rlim_max;
545 error = kern_setrlimit(td, uap->which, &lim);
549 #ifndef _SYS_SYSPROTO_H_
550 struct ogetrlimit_args {
558 register struct ogetrlimit_args *uap;
565 if (uap->which >= RLIM_NLIMITS)
569 lim_rlimit(p, uap->which, &rl);
573 * XXX would be more correct to convert only RLIM_INFINITY to the
574 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
575 * values. Most 64->32 and 32->16 conversions, including not
576 * unimportant ones of uids are even more broken than what we
577 * do here (they blindly truncate). We don't do this correctly
578 * here since we have little experience with EOVERFLOW yet.
579 * Elsewhere, getuid() can't fail...
581 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
582 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
583 error = copyout(&olim, uap->rlp, sizeof(olim));
586 #endif /* COMPAT_43 */
588 #ifndef _SYS_SYSPROTO_H_
589 struct __setrlimit_args {
597 register struct __setrlimit_args *uap;
602 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
604 error = kern_setrlimit(td, uap->which, &alim);
616 PROC_LOCK_ASSERT(p, MA_OWNED);
618 * Check if the process exceeds its cpu resource allocation. If
619 * it reaches the max, arrange to kill the process in ast().
621 if (p->p_cpulimit == RLIM_INFINITY)
624 FOREACH_THREAD_IN_PROC(p, td) {
628 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
629 lim_rlimit(p, RLIMIT_CPU, &rlim);
630 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
631 killproc(p, "exceeded maximum CPU limit");
633 if (p->p_cpulimit < rlim.rlim_max)
638 if ((p->p_flag & P_WEXIT) == 0)
639 callout_reset(&p->p_limco, hz, lim_cb, p);
643 kern_setrlimit(td, which, limp)
648 struct plimit *newlim, *oldlim;
650 register struct rlimit *alimp;
651 struct rlimit oldssiz;
654 if (which >= RLIM_NLIMITS)
658 * Preserve historical bugs by treating negative limits as unsigned.
660 if (limp->rlim_cur < 0)
661 limp->rlim_cur = RLIM_INFINITY;
662 if (limp->rlim_max < 0)
663 limp->rlim_max = RLIM_INFINITY;
665 oldssiz.rlim_cur = 0;
667 newlim = lim_alloc();
670 alimp = &oldlim->pl_rlimit[which];
671 if (limp->rlim_cur > alimp->rlim_max ||
672 limp->rlim_max > alimp->rlim_max)
673 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
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 if (limp->rlim_cur != RLIM_INFINITY &&
687 p->p_cpulimit == RLIM_INFINITY)
688 callout_reset(&p->p_limco, hz, lim_cb, p);
689 p->p_cpulimit = limp->rlim_cur;
692 if (limp->rlim_cur > maxdsiz)
693 limp->rlim_cur = maxdsiz;
694 if (limp->rlim_max > maxdsiz)
695 limp->rlim_max = maxdsiz;
699 if (limp->rlim_cur > maxssiz)
700 limp->rlim_cur = maxssiz;
701 if (limp->rlim_max > maxssiz)
702 limp->rlim_max = maxssiz;
704 if (p->p_sysent->sv_fixlimit != NULL)
705 p->p_sysent->sv_fixlimit(&oldssiz,
710 if (limp->rlim_cur > maxfilesperproc)
711 limp->rlim_cur = maxfilesperproc;
712 if (limp->rlim_max > maxfilesperproc)
713 limp->rlim_max = maxfilesperproc;
717 if (limp->rlim_cur > maxprocperuid)
718 limp->rlim_cur = maxprocperuid;
719 if (limp->rlim_max > maxprocperuid)
720 limp->rlim_max = maxprocperuid;
721 if (limp->rlim_cur < 1)
723 if (limp->rlim_max < 1)
727 if (p->p_sysent->sv_fixlimit != NULL)
728 p->p_sysent->sv_fixlimit(limp, which);
734 if (which == RLIMIT_STACK) {
736 * Stack is allocated to the max at exec time with only
737 * "rlim_cur" bytes accessible. If stack limit is going
738 * up make more accessible, if going down make inaccessible.
740 if (limp->rlim_cur != oldssiz.rlim_cur) {
745 if (limp->rlim_cur > oldssiz.rlim_cur) {
746 prot = p->p_sysent->sv_stackprot;
747 size = limp->rlim_cur - oldssiz.rlim_cur;
748 addr = p->p_sysent->sv_usrstack -
752 size = oldssiz.rlim_cur - limp->rlim_cur;
753 addr = p->p_sysent->sv_usrstack -
756 addr = trunc_page(addr);
757 size = round_page(size);
758 (void)vm_map_protect(&p->p_vmspace->vm_map,
759 addr, addr + size, prot, FALSE);
766 #ifndef _SYS_SYSPROTO_H_
767 struct __getrlimit_args {
776 register struct __getrlimit_args *uap;
782 if (uap->which >= RLIM_NLIMITS)
786 lim_rlimit(p, uap->which, &rlim);
788 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
793 * Transform the running time and tick information for children of proc p
794 * into user and system time usage.
803 PROC_LOCK_ASSERT(p, MA_OWNED);
804 calcru1(p, &p->p_crux, up, sp);
808 * Transform the running time and tick information in proc p into user
809 * and system time usage. If appropriate, include the current time slice
813 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
818 PROC_LOCK_ASSERT(p, MA_OWNED);
819 PROC_SLOCK_ASSERT(p, MA_OWNED);
821 * If we are getting stats for the current process, then add in the
822 * stats that this thread has accumulated in its current time slice.
823 * We reset the thread and CPU state as if we had performed a context
827 if (td->td_proc == p) {
829 runtime = u - PCPU_GET(switchtime);
830 td->td_runtime += runtime;
831 td->td_incruntime += runtime;
832 PCPU_SET(switchtime, u);
834 /* Make sure the per-thread stats are current. */
835 FOREACH_THREAD_IN_PROC(p, td) {
836 if (td->td_incruntime == 0)
840 calcru1(p, &p->p_rux, up, sp);
843 /* Collect resource usage for a single thread. */
845 rufetchtd(struct thread *td, struct rusage *ru)
851 PROC_SLOCK_ASSERT(p, MA_OWNED);
852 THREAD_LOCK_ASSERT(td, MA_OWNED);
854 * If we are getting stats for the current thread, then add in the
855 * stats that this thread has accumulated in its current time slice.
856 * We reset the thread and CPU state as if we had performed a context
859 if (td == curthread) {
861 runtime = u - PCPU_GET(switchtime);
862 td->td_runtime += runtime;
863 td->td_incruntime += runtime;
864 PCPU_SET(switchtime, u);
868 calcru1(p, &td->td_rux, &ru->ru_utime, &ru->ru_stime);
872 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
875 /* {user, system, interrupt, total} {ticks, usec}: */
876 uint64_t ut, uu, st, su, it, tt, tu;
878 ut = ruxp->rux_uticks;
879 st = ruxp->rux_sticks;
880 it = ruxp->rux_iticks;
883 /* Avoid divide by zero */
887 tu = cputick2usec(ruxp->rux_runtime);
888 if ((int64_t)tu < 0) {
889 /* XXX: this should be an assert /phk */
890 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
891 (intmax_t)tu, p->p_pid, p->p_comm);
895 if (tu >= ruxp->rux_tu) {
897 * The normal case, time increased.
898 * Enforce monotonicity of bucketed numbers.
901 if (uu < ruxp->rux_uu)
904 if (su < ruxp->rux_su)
906 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
908 * When we calibrate the cputicker, it is not uncommon to
909 * see the presumably fixed frequency increase slightly over
910 * time as a result of thermal stabilization and NTP
911 * discipline (of the reference clock). We therefore ignore
912 * a bit of backwards slop because we expect to catch up
913 * shortly. We use a 3 microsecond limit to catch low
914 * counts and a 1% limit for high counts.
919 } else { /* tu < ruxp->rux_tu */
921 * What happened here was likely that a laptop, which ran at
922 * a reduced clock frequency at boot, kicked into high gear.
923 * The wisdom of spamming this message in that case is
924 * dubious, but it might also be indicative of something
925 * serious, so lets keep it and hope laptops can be made
926 * more truthful about their CPU speed via ACPI.
928 printf("calcru: runtime went backwards from %ju usec "
929 "to %ju usec for pid %d (%s)\n",
930 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
931 p->p_pid, p->p_comm);
940 up->tv_sec = uu / 1000000;
941 up->tv_usec = uu % 1000000;
942 sp->tv_sec = su / 1000000;
943 sp->tv_usec = su % 1000000;
946 #ifndef _SYS_SYSPROTO_H_
947 struct getrusage_args {
949 struct rusage *rusage;
954 register struct thread *td;
955 register struct getrusage_args *uap;
960 error = kern_getrusage(td, uap->who, &ru);
962 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
967 kern_getrusage(struct thread *td, int who, struct rusage *rup)
977 rufetchcalc(p, rup, &rup->ru_utime,
981 case RUSAGE_CHILDREN:
982 *rup = p->p_stats->p_cru;
983 calccru(p, &rup->ru_utime, &rup->ru_stime);
1002 rucollect(struct rusage *ru, struct rusage *ru2)
1007 if (ru->ru_maxrss < ru2->ru_maxrss)
1008 ru->ru_maxrss = ru2->ru_maxrss;
1010 ip2 = &ru2->ru_first;
1011 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1016 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1017 struct rusage_ext *rux2)
1020 rux->rux_runtime += rux2->rux_runtime;
1021 rux->rux_uticks += rux2->rux_uticks;
1022 rux->rux_sticks += rux2->rux_sticks;
1023 rux->rux_iticks += rux2->rux_iticks;
1024 rux->rux_uu += rux2->rux_uu;
1025 rux->rux_su += rux2->rux_su;
1026 rux->rux_tu += rux2->rux_tu;
1031 * Aggregate tick counts into the proc's rusage_ext.
1034 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1037 THREAD_LOCK_ASSERT(td, MA_OWNED);
1038 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1039 rux->rux_runtime += td->td_incruntime;
1040 rux->rux_uticks += td->td_uticks;
1041 rux->rux_sticks += td->td_sticks;
1042 rux->rux_iticks += td->td_iticks;
1046 ruxagg(struct proc *p, struct thread *td)
1050 ruxagg_locked(&p->p_rux, td);
1051 ruxagg_locked(&td->td_rux, td);
1052 td->td_incruntime = 0;
1060 * Update the rusage_ext structure and fetch a valid aggregate rusage
1061 * for proc p if storage for one is supplied.
1064 rufetch(struct proc *p, struct rusage *ru)
1068 PROC_SLOCK_ASSERT(p, MA_OWNED);
1071 if (p->p_numthreads > 0) {
1072 FOREACH_THREAD_IN_PROC(p, td) {
1074 rucollect(ru, &td->td_ru);
1080 * Atomically perform a rufetch and a calcru together.
1081 * Consumers, can safely assume the calcru is executed only once
1082 * rufetch is completed.
1085 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1096 * Allocate a new resource limits structure and initialize its
1097 * reference count and mutex pointer.
1102 struct plimit *limp;
1104 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1105 refcount_init(&limp->pl_refcnt, 1);
1111 struct plimit *limp;
1114 refcount_acquire(&limp->pl_refcnt);
1119 lim_fork(struct proc *p1, struct proc *p2)
1121 p2->p_limit = lim_hold(p1->p_limit);
1122 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1123 if (p1->p_cpulimit != RLIM_INFINITY)
1124 callout_reset(&p2->p_limco, hz, lim_cb, p2);
1129 struct plimit *limp;
1132 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1133 if (refcount_release(&limp->pl_refcnt))
1134 free((void *)limp, M_PLIMIT);
1138 * Make a copy of the plimit structure.
1139 * We share these structures copy-on-write after fork.
1143 struct plimit *dst, *src;
1146 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1147 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1151 * Return the hard limit for a particular system resource. The
1152 * which parameter specifies the index into the rlimit array.
1155 lim_max(struct proc *p, int which)
1159 lim_rlimit(p, which, &rl);
1160 return (rl.rlim_max);
1164 * Return the current (soft) limit for a particular system resource.
1165 * The which parameter which specifies the index into the rlimit array
1168 lim_cur(struct proc *p, int which)
1172 lim_rlimit(p, which, &rl);
1173 return (rl.rlim_cur);
1177 * Return a copy of the entire rlimit structure for the system limit
1178 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1181 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1184 PROC_LOCK_ASSERT(p, MA_OWNED);
1185 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1186 ("request for invalid resource limit"));
1187 *rlp = p->p_limit->pl_rlimit[which];
1188 if (p->p_sysent->sv_fixlimit != NULL)
1189 p->p_sysent->sv_fixlimit(rlp, which);
1196 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1197 rw_init(&uihashtbl_lock, "uidinfo hash");
1201 * Look up a uidinfo struct for the parameter uid.
1202 * uihashtbl_lock must be locked.
1204 static struct uidinfo *
1208 struct uihashhead *uipp;
1209 struct uidinfo *uip;
1211 rw_assert(&uihashtbl_lock, RA_LOCKED);
1213 LIST_FOREACH(uip, uipp, ui_hash)
1214 if (uip->ui_uid == uid)
1221 * Find or allocate a struct uidinfo for a particular uid.
1222 * Increase refcount on uidinfo struct returned.
1223 * uifree() should be called on a struct uidinfo when released.
1229 struct uidinfo *old_uip, *uip;
1231 rw_rlock(&uihashtbl_lock);
1232 uip = uilookup(uid);
1234 rw_runlock(&uihashtbl_lock);
1235 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1236 racct_create(&uip->ui_racct);
1237 rw_wlock(&uihashtbl_lock);
1239 * There's a chance someone created our uidinfo while we
1240 * were in malloc and not holding the lock, so we have to
1241 * make sure we don't insert a duplicate uidinfo.
1243 if ((old_uip = uilookup(uid)) != NULL) {
1244 /* Someone else beat us to it. */
1245 racct_destroy(&uip->ui_racct);
1246 free(uip, M_UIDINFO);
1249 refcount_init(&uip->ui_ref, 0);
1251 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1253 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1257 rw_unlock(&uihashtbl_lock);
1262 * Place another refcount on a uidinfo struct.
1266 struct uidinfo *uip;
1269 refcount_acquire(&uip->ui_ref);
1273 * Since uidinfo structs have a long lifetime, we use an
1274 * opportunistic refcounting scheme to avoid locking the lookup hash
1277 * If the refcount hits 0, we need to free the structure,
1278 * which means we need to lock the hash.
1280 * After locking the struct and lowering the refcount, if we find
1281 * that we don't need to free, simply unlock and return.
1283 * If refcount lowering results in need to free, bump the count
1284 * back up, lose the lock and acquire the locks in the proper
1285 * order to try again.
1289 struct uidinfo *uip;
1293 /* Prepare for optimal case. */
1295 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1298 /* Prepare for suboptimal case. */
1299 rw_wlock(&uihashtbl_lock);
1300 if (refcount_release(&uip->ui_ref)) {
1301 racct_destroy(&uip->ui_racct);
1302 LIST_REMOVE(uip, ui_hash);
1303 rw_wunlock(&uihashtbl_lock);
1304 if (uip->ui_sbsize != 0)
1305 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1306 uip->ui_uid, uip->ui_sbsize);
1307 if (uip->ui_proccnt != 0)
1308 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1309 uip->ui_uid, uip->ui_proccnt);
1310 if (uip->ui_vmsize != 0)
1311 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1312 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1313 mtx_destroy(&uip->ui_vmsize_mtx);
1314 free(uip, M_UIDINFO);
1318 * Someone added a reference between atomic_cmpset_int() and
1319 * rw_wlock(&uihashtbl_lock).
1321 rw_wunlock(&uihashtbl_lock);
1325 ui_racct_foreach(void (*callback)(struct racct *racct,
1326 void *arg2, void *arg3), void *arg2, void *arg3)
1328 struct uidinfo *uip;
1329 struct uihashhead *uih;
1331 rw_rlock(&uihashtbl_lock);
1332 for (uih = &uihashtbl[uihash]; uih >= uihashtbl; uih--) {
1333 LIST_FOREACH(uip, uih, ui_hash) {
1334 (callback)(uip->ui_racct, arg2, arg3);
1337 rw_runlock(&uihashtbl_lock);
1341 * Change the count associated with number of processes
1342 * a given user is using. When 'max' is 0, don't enforce a limit
1345 chgproccnt(uip, diff, max)
1346 struct uidinfo *uip;
1351 /* Don't allow them to exceed max, but allow subtraction. */
1352 if (diff > 0 && max != 0) {
1353 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1354 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1358 atomic_add_long(&uip->ui_proccnt, (long)diff);
1359 if (uip->ui_proccnt < 0)
1360 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1366 * Change the total socket buffer size a user has used.
1369 chgsbsize(uip, hiwat, to, max)
1370 struct uidinfo *uip;
1379 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1380 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1384 atomic_add_long(&uip->ui_sbsize, (long)diff);
1385 if (uip->ui_sbsize < 0)
1386 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1393 * Change the count associated with number of pseudo-terminals
1394 * a given user is using. When 'max' is 0, don't enforce a limit
1397 chgptscnt(uip, diff, max)
1398 struct uidinfo *uip;
1403 /* Don't allow them to exceed max, but allow subtraction. */
1404 if (diff > 0 && max != 0) {
1405 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1406 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1410 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1411 if (uip->ui_ptscnt < 0)
1412 printf("negative ptscnt for uid = %d\n", uip->ui_uid);