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_cansee(td, p) == 0) {
142 uap->who = td->td_ucred->cr_uid;
143 sx_slock(&allproc_lock);
144 FOREACH_PROC_IN_SYSTEM(p) {
145 /* Do not bother to check PRS_NEW processes */
146 if (p->p_state == PRS_NEW)
149 if (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_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_ucred->cr_uid == uap->who &&
235 p_cansee(td, p) == 0) {
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)
275 * Set realtime priority for LWP.
277 #ifndef _SYS_SYSPROTO_H_
278 struct rtprio_thread_args {
285 rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
292 /* Perform copyin before acquiring locks if needed. */
293 if (uap->function == RTP_SET)
294 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 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
312 td1 = thread_find(p, uap->lwpid);
314 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);
341 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
344 td1 = thread_find(p, uap->lwpid);
346 error = rtp_to_pri(&rtp, td1);
359 * Set realtime priority.
361 #ifndef _SYS_SYSPROTO_H_
370 struct thread *td; /* curthread */
371 register struct rtprio_args *uap;
378 /* Perform copyin before acquiring locks if needed. */
379 if (uap->function == RTP_SET)
380 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
393 switch (uap->function) {
395 if ((error = p_cansee(td, p)))
398 * Return OUR priority if no pid specified,
399 * or if one is, report the highest priority
400 * in the process. There isn't much more you can do as
401 * there is only room to return a single priority.
402 * Note: specifying our own pid is not the same
403 * as leaving it zero.
406 pri_to_rtp(td, &rtp);
410 rtp.type = RTP_PRIO_IDLE;
411 rtp.prio = RTP_PRIO_MAX;
412 FOREACH_THREAD_IN_PROC(p, tdp) {
413 pri_to_rtp(tdp, &rtp2);
414 if (rtp2.type < rtp.type ||
415 (rtp2.type == rtp.type &&
416 rtp2.prio < rtp.prio)) {
417 rtp.type = rtp2.type;
418 rtp.prio = rtp2.prio;
423 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
425 if ((error = p_cansched(td, p)) || (error = cierror))
428 /* Disallow setting rtprio in most cases if not superuser. */
430 * Realtime priority has to be restricted for reasons which should be
431 * obvious. However, for idle priority, there is a potential for
432 * system deadlock if an idleprio process gains a lock on a resource
433 * that other processes need (and the idleprio process can't run
434 * due to a CPU-bound normal process). Fix me! XXX
437 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
439 if (rtp.type != RTP_PRIO_NORMAL) {
441 error = priv_check(td, PRIV_SCHED_RTPRIO);
447 * If we are setting our own priority, set just our
448 * thread but if we are doing another process,
449 * do all the threads on that process. If we
450 * specify our own pid we do the latter.
453 error = rtp_to_pri(&rtp, td);
455 FOREACH_THREAD_IN_PROC(p, td) {
456 if ((error = rtp_to_pri(&rtp, td)) != 0)
470 rtp_to_pri(struct rtprio *rtp, struct thread *td)
475 switch (RTP_PRIO_BASE(rtp->type)) {
476 case RTP_PRIO_REALTIME:
477 if (rtp->prio > RTP_PRIO_MAX)
479 newpri = PRI_MIN_REALTIME + rtp->prio;
481 case RTP_PRIO_NORMAL:
482 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
484 newpri = PRI_MIN_TIMESHARE + rtp->prio;
487 if (rtp->prio > RTP_PRIO_MAX)
489 newpri = PRI_MIN_IDLE + rtp->prio;
496 sched_class(td, rtp->type); /* XXX fix */
497 oldpri = td->td_user_pri;
498 sched_user_prio(td, newpri);
500 sched_prio(curthread, td->td_user_pri); /* XXX dubious */
501 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
503 umtx_pi_adjust(td, oldpri);
510 pri_to_rtp(struct thread *td, struct rtprio *rtp)
514 switch (PRI_BASE(td->td_pri_class)) {
516 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
519 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
522 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
527 rtp->type = td->td_pri_class;
531 #if defined(COMPAT_43)
532 #ifndef _SYS_SYSPROTO_H_
533 struct osetrlimit_args {
541 register struct osetrlimit_args *uap;
547 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
549 lim.rlim_cur = olim.rlim_cur;
550 lim.rlim_max = olim.rlim_max;
551 error = kern_setrlimit(td, uap->which, &lim);
555 #ifndef _SYS_SYSPROTO_H_
556 struct ogetrlimit_args {
564 register struct ogetrlimit_args *uap;
571 if (uap->which >= RLIM_NLIMITS)
575 lim_rlimit(p, uap->which, &rl);
579 * XXX would be more correct to convert only RLIM_INFINITY to the
580 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
581 * values. Most 64->32 and 32->16 conversions, including not
582 * unimportant ones of uids are even more broken than what we
583 * do here (they blindly truncate). We don't do this correctly
584 * here since we have little experience with EOVERFLOW yet.
585 * Elsewhere, getuid() can't fail...
587 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
588 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
589 error = copyout(&olim, uap->rlp, sizeof(olim));
592 #endif /* COMPAT_43 */
594 #ifndef _SYS_SYSPROTO_H_
595 struct __setrlimit_args {
603 register struct __setrlimit_args *uap;
608 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
610 error = kern_setrlimit(td, uap->which, &alim);
622 PROC_LOCK_ASSERT(p, MA_OWNED);
624 * Check if the process exceeds its cpu resource allocation. If
625 * it reaches the max, arrange to kill the process in ast().
627 if (p->p_cpulimit == RLIM_INFINITY)
630 FOREACH_THREAD_IN_PROC(p, td) {
634 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
635 lim_rlimit(p, RLIMIT_CPU, &rlim);
636 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
637 killproc(p, "exceeded maximum CPU limit");
639 if (p->p_cpulimit < rlim.rlim_max)
644 if ((p->p_flag & P_WEXIT) == 0)
645 callout_reset(&p->p_limco, hz, lim_cb, p);
649 kern_setrlimit(td, which, limp)
654 struct plimit *newlim, *oldlim;
656 register struct rlimit *alimp;
657 struct rlimit oldssiz;
660 if (which >= RLIM_NLIMITS)
664 * Preserve historical bugs by treating negative limits as unsigned.
666 if (limp->rlim_cur < 0)
667 limp->rlim_cur = RLIM_INFINITY;
668 if (limp->rlim_max < 0)
669 limp->rlim_max = RLIM_INFINITY;
671 oldssiz.rlim_cur = 0;
673 newlim = lim_alloc();
676 alimp = &oldlim->pl_rlimit[which];
677 if (limp->rlim_cur > alimp->rlim_max ||
678 limp->rlim_max > alimp->rlim_max)
679 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
684 if (limp->rlim_cur > limp->rlim_max)
685 limp->rlim_cur = limp->rlim_max;
686 lim_copy(newlim, oldlim);
687 alimp = &newlim->pl_rlimit[which];
692 if (limp->rlim_cur != RLIM_INFINITY &&
693 p->p_cpulimit == RLIM_INFINITY)
694 callout_reset(&p->p_limco, hz, lim_cb, p);
695 p->p_cpulimit = limp->rlim_cur;
698 if (limp->rlim_cur > maxdsiz)
699 limp->rlim_cur = maxdsiz;
700 if (limp->rlim_max > maxdsiz)
701 limp->rlim_max = maxdsiz;
705 if (limp->rlim_cur > maxssiz)
706 limp->rlim_cur = maxssiz;
707 if (limp->rlim_max > maxssiz)
708 limp->rlim_max = maxssiz;
710 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
711 td->td_proc->p_sysent->sv_fixlimit(&oldssiz,
716 if (limp->rlim_cur > maxfilesperproc)
717 limp->rlim_cur = maxfilesperproc;
718 if (limp->rlim_max > maxfilesperproc)
719 limp->rlim_max = maxfilesperproc;
723 if (limp->rlim_cur > maxprocperuid)
724 limp->rlim_cur = maxprocperuid;
725 if (limp->rlim_max > maxprocperuid)
726 limp->rlim_max = maxprocperuid;
727 if (limp->rlim_cur < 1)
729 if (limp->rlim_max < 1)
733 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
734 td->td_proc->p_sysent->sv_fixlimit(limp, which);
740 if (which == RLIMIT_STACK) {
742 * Stack is allocated to the max at exec time with only
743 * "rlim_cur" bytes accessible. If stack limit is going
744 * up make more accessible, if going down make inaccessible.
746 if (limp->rlim_cur != oldssiz.rlim_cur) {
751 if (limp->rlim_cur > oldssiz.rlim_cur) {
752 prot = p->p_sysent->sv_stackprot;
753 size = limp->rlim_cur - oldssiz.rlim_cur;
754 addr = p->p_sysent->sv_usrstack -
758 size = oldssiz.rlim_cur - limp->rlim_cur;
759 addr = p->p_sysent->sv_usrstack -
762 addr = trunc_page(addr);
763 size = round_page(size);
764 (void)vm_map_protect(&p->p_vmspace->vm_map,
765 addr, addr + size, prot, FALSE);
772 #ifndef _SYS_SYSPROTO_H_
773 struct __getrlimit_args {
782 register struct __getrlimit_args *uap;
788 if (uap->which >= RLIM_NLIMITS)
792 lim_rlimit(p, uap->which, &rlim);
794 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
799 * Transform the running time and tick information for children of proc p
800 * into user and system time usage.
809 PROC_LOCK_ASSERT(p, MA_OWNED);
810 calcru1(p, &p->p_crux, up, sp);
814 * Transform the running time and tick information in proc p into user
815 * and system time usage. If appropriate, include the current time slice
819 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
824 PROC_LOCK_ASSERT(p, MA_OWNED);
825 PROC_SLOCK_ASSERT(p, MA_OWNED);
827 * If we are getting stats for the current process, then add in the
828 * stats that this thread has accumulated in its current time slice.
829 * We reset the thread and CPU state as if we had performed a context
833 if (td->td_proc == p) {
835 p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
836 PCPU_SET(switchtime, u);
838 /* Make sure the per-thread stats are current. */
839 FOREACH_THREAD_IN_PROC(p, td) {
840 if (td->td_incruntime == 0)
844 calcru1(p, &p->p_rux, up, sp);
848 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
851 /* {user, system, interrupt, total} {ticks, usec}: */
852 u_int64_t ut, uu, st, su, it, tt, tu;
854 ut = ruxp->rux_uticks;
855 st = ruxp->rux_sticks;
856 it = ruxp->rux_iticks;
859 /* Avoid divide by zero */
863 tu = cputick2usec(ruxp->rux_runtime);
864 if ((int64_t)tu < 0) {
865 /* XXX: this should be an assert /phk */
866 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
867 (intmax_t)tu, p->p_pid, p->p_comm);
871 if (tu >= ruxp->rux_tu) {
873 * The normal case, time increased.
874 * Enforce monotonicity of bucketed numbers.
877 if (uu < ruxp->rux_uu)
880 if (su < ruxp->rux_su)
882 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
884 * When we calibrate the cputicker, it is not uncommon to
885 * see the presumably fixed frequency increase slightly over
886 * time as a result of thermal stabilization and NTP
887 * discipline (of the reference clock). We therefore ignore
888 * a bit of backwards slop because we expect to catch up
889 * shortly. We use a 3 microsecond limit to catch low
890 * counts and a 1% limit for high counts.
895 } else { /* tu < ruxp->rux_tu */
897 * What happened here was likely that a laptop, which ran at
898 * a reduced clock frequency at boot, kicked into high gear.
899 * The wisdom of spamming this message in that case is
900 * dubious, but it might also be indicative of something
901 * serious, so lets keep it and hope laptops can be made
902 * more truthful about their CPU speed via ACPI.
904 printf("calcru: runtime went backwards from %ju usec "
905 "to %ju usec for pid %d (%s)\n",
906 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
907 p->p_pid, p->p_comm);
916 up->tv_sec = uu / 1000000;
917 up->tv_usec = uu % 1000000;
918 sp->tv_sec = su / 1000000;
919 sp->tv_usec = su % 1000000;
922 #ifndef _SYS_SYSPROTO_H_
923 struct getrusage_args {
925 struct rusage *rusage;
930 register struct thread *td;
931 register struct getrusage_args *uap;
936 error = kern_getrusage(td, uap->who, &ru);
938 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
943 kern_getrusage(struct thread *td, int who, struct rusage *rup)
953 rufetchcalc(p, rup, &rup->ru_utime,
957 case RUSAGE_CHILDREN:
958 *rup = p->p_stats->p_cru;
959 calccru(p, &rup->ru_utime, &rup->ru_stime);
968 calcru1(p, &td->td_rux, &rup->ru_utime, &rup->ru_stime);
980 rucollect(struct rusage *ru, struct rusage *ru2)
985 if (ru->ru_maxrss < ru2->ru_maxrss)
986 ru->ru_maxrss = ru2->ru_maxrss;
988 ip2 = &ru2->ru_first;
989 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
994 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
995 struct rusage_ext *rux2)
998 rux->rux_runtime += rux2->rux_runtime;
999 rux->rux_uticks += rux2->rux_uticks;
1000 rux->rux_sticks += rux2->rux_sticks;
1001 rux->rux_iticks += rux2->rux_iticks;
1002 rux->rux_uu += rux2->rux_uu;
1003 rux->rux_su += rux2->rux_su;
1004 rux->rux_tu += rux2->rux_tu;
1009 * Aggregate tick counts into the proc's rusage_ext.
1012 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1015 THREAD_LOCK_ASSERT(td, MA_OWNED);
1016 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1017 rux->rux_runtime += td->td_incruntime;
1018 rux->rux_uticks += td->td_uticks;
1019 rux->rux_sticks += td->td_sticks;
1020 rux->rux_iticks += td->td_iticks;
1024 ruxagg(struct proc *p, struct thread *td)
1028 ruxagg_locked(&p->p_rux, td);
1029 ruxagg_locked(&td->td_rux, td);
1030 td->td_incruntime = 0;
1038 * Update the rusage_ext structure and fetch a valid aggregate rusage
1039 * for proc p if storage for one is supplied.
1042 rufetch(struct proc *p, struct rusage *ru)
1046 PROC_SLOCK_ASSERT(p, MA_OWNED);
1049 if (p->p_numthreads > 0) {
1050 FOREACH_THREAD_IN_PROC(p, td) {
1052 rucollect(ru, &td->td_ru);
1058 * Atomically perform a rufetch and a calcru together.
1059 * Consumers, can safely assume the calcru is executed only once
1060 * rufetch is completed.
1063 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1074 * Allocate a new resource limits structure and initialize its
1075 * reference count and mutex pointer.
1080 struct plimit *limp;
1082 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1083 refcount_init(&limp->pl_refcnt, 1);
1089 struct plimit *limp;
1092 refcount_acquire(&limp->pl_refcnt);
1097 lim_fork(struct proc *p1, struct proc *p2)
1099 p2->p_limit = lim_hold(p1->p_limit);
1100 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1101 if (p1->p_cpulimit != RLIM_INFINITY)
1102 callout_reset(&p2->p_limco, hz, lim_cb, p2);
1107 struct plimit *limp;
1110 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1111 if (refcount_release(&limp->pl_refcnt))
1112 free((void *)limp, M_PLIMIT);
1116 * Make a copy of the plimit structure.
1117 * We share these structures copy-on-write after fork.
1121 struct plimit *dst, *src;
1124 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1125 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1129 * Return the hard limit for a particular system resource. The
1130 * which parameter specifies the index into the rlimit array.
1133 lim_max(struct proc *p, int which)
1137 lim_rlimit(p, which, &rl);
1138 return (rl.rlim_max);
1142 * Return the current (soft) limit for a particular system resource.
1143 * The which parameter which specifies the index into the rlimit array
1146 lim_cur(struct proc *p, int which)
1150 lim_rlimit(p, which, &rl);
1151 return (rl.rlim_cur);
1155 * Return a copy of the entire rlimit structure for the system limit
1156 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1159 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1162 PROC_LOCK_ASSERT(p, MA_OWNED);
1163 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1164 ("request for invalid resource limit"));
1165 *rlp = p->p_limit->pl_rlimit[which];
1166 if (p->p_sysent->sv_fixlimit != NULL)
1167 p->p_sysent->sv_fixlimit(rlp, which);
1171 * Find the uidinfo structure for a uid. This structure is used to
1172 * track the total resource consumption (process count, socket buffer
1173 * size, etc.) for the uid and impose limits.
1179 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1180 rw_init(&uihashtbl_lock, "uidinfo hash");
1184 * Look up a uidinfo struct for the parameter uid.
1185 * uihashtbl_lock must be locked.
1187 static struct uidinfo *
1191 struct uihashhead *uipp;
1192 struct uidinfo *uip;
1194 rw_assert(&uihashtbl_lock, RA_LOCKED);
1196 LIST_FOREACH(uip, uipp, ui_hash)
1197 if (uip->ui_uid == uid)
1204 * Find or allocate a struct uidinfo for a particular uid.
1205 * Increase refcount on uidinfo struct returned.
1206 * uifree() should be called on a struct uidinfo when released.
1212 struct uidinfo *old_uip, *uip;
1214 rw_rlock(&uihashtbl_lock);
1215 uip = uilookup(uid);
1217 rw_runlock(&uihashtbl_lock);
1218 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1219 rw_wlock(&uihashtbl_lock);
1221 * There's a chance someone created our uidinfo while we
1222 * were in malloc and not holding the lock, so we have to
1223 * make sure we don't insert a duplicate uidinfo.
1225 if ((old_uip = uilookup(uid)) != NULL) {
1226 /* Someone else beat us to it. */
1227 free(uip, M_UIDINFO);
1230 refcount_init(&uip->ui_ref, 0);
1232 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1234 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1238 rw_unlock(&uihashtbl_lock);
1243 * Place another refcount on a uidinfo struct.
1247 struct uidinfo *uip;
1250 refcount_acquire(&uip->ui_ref);
1254 * Since uidinfo structs have a long lifetime, we use an
1255 * opportunistic refcounting scheme to avoid locking the lookup hash
1258 * If the refcount hits 0, we need to free the structure,
1259 * which means we need to lock the hash.
1261 * After locking the struct and lowering the refcount, if we find
1262 * that we don't need to free, simply unlock and return.
1264 * If refcount lowering results in need to free, bump the count
1265 * back up, lose the lock and acquire the locks in the proper
1266 * order to try again.
1270 struct uidinfo *uip;
1274 /* Prepare for optimal case. */
1276 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1279 /* Prepare for suboptimal case. */
1280 rw_wlock(&uihashtbl_lock);
1281 if (refcount_release(&uip->ui_ref)) {
1282 LIST_REMOVE(uip, ui_hash);
1283 rw_wunlock(&uihashtbl_lock);
1284 if (uip->ui_sbsize != 0)
1285 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1286 uip->ui_uid, uip->ui_sbsize);
1287 if (uip->ui_proccnt != 0)
1288 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1289 uip->ui_uid, uip->ui_proccnt);
1290 if (uip->ui_vmsize != 0)
1291 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1292 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1293 mtx_destroy(&uip->ui_vmsize_mtx);
1294 free(uip, M_UIDINFO);
1298 * Someone added a reference between atomic_cmpset_int() and
1299 * rw_wlock(&uihashtbl_lock).
1301 rw_wunlock(&uihashtbl_lock);
1305 * Change the count associated with number of processes
1306 * a given user is using. When 'max' is 0, don't enforce a limit
1309 chgproccnt(uip, diff, max)
1310 struct uidinfo *uip;
1315 /* Don't allow them to exceed max, but allow subtraction. */
1316 if (diff > 0 && max != 0) {
1317 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1318 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1322 atomic_add_long(&uip->ui_proccnt, (long)diff);
1323 if (uip->ui_proccnt < 0)
1324 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1330 * Change the total socket buffer size a user has used.
1333 chgsbsize(uip, hiwat, to, max)
1334 struct uidinfo *uip;
1343 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1344 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1348 atomic_add_long(&uip->ui_sbsize, (long)diff);
1349 if (uip->ui_sbsize < 0)
1350 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1357 * Change the count associated with number of pseudo-terminals
1358 * a given user is using. When 'max' is 0, don't enforce a limit
1361 chgptscnt(uip, diff, max)
1362 struct uidinfo *uip;
1367 /* Don't allow them to exceed max, but allow subtraction. */
1368 if (diff > 0 && max != 0) {
1369 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1370 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1374 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1375 if (uip->ui_ptscnt < 0)
1376 printf("negative ptscnt for uid = %d\n", uip->ui_uid);