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/sysctl.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)
276 static int unprivileged_idprio;
277 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_idprio, CTLFLAG_RW,
278 &unprivileged_idprio, 0, "Allow non-root users to set an idle priority");
281 * Set realtime priority for LWP.
283 #ifndef _SYS_SYSPROTO_H_
284 struct rtprio_thread_args {
291 rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
298 /* Perform copyin before acquiring locks if needed. */
299 if (uap->function == RTP_SET)
300 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
305 * Though lwpid is unique, only current process is supported
306 * since there is no efficient way to look up a LWP yet.
311 switch (uap->function) {
313 if ((error = p_cansee(td, p)))
315 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
318 td1 = thread_find(p, uap->lwpid);
320 pri_to_rtp(td1, &rtp);
324 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
326 if ((error = p_cansched(td, p)) || (error = cierror))
329 /* Disallow setting rtprio in most cases if not superuser. */
332 * Realtime priority has to be restricted for reasons which
333 * should be obvious. However, for idleprio processes, there is
334 * a potential for system deadlock if an idleprio process gains
335 * a lock on a resource that other processes need (and the
336 * idleprio process can't run due to a CPU-bound normal
337 * process). Fix me! XXX
339 * This problem is not only related to idleprio process.
340 * A user level program can obtain a file lock and hold it
341 * indefinitely. Additionally, without idleprio processes it is
342 * still conceivable that a program with low priority will never
343 * get to run. In short, allowing this feature might make it
344 * easier to lock a resource indefinitely, but it is not the
345 * only thing that makes it possible.
347 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME ||
348 (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
349 unprivileged_idprio == 0)) {
350 error = priv_check(td, PRIV_SCHED_RTPRIO);
355 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
358 td1 = thread_find(p, uap->lwpid);
360 error = rtp_to_pri(&rtp, td1);
373 * Set realtime priority.
375 #ifndef _SYS_SYSPROTO_H_
384 struct thread *td; /* curthread */
385 register struct rtprio_args *uap;
392 /* Perform copyin before acquiring locks if needed. */
393 if (uap->function == RTP_SET)
394 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
407 switch (uap->function) {
409 if ((error = p_cansee(td, p)))
412 * Return OUR priority if no pid specified,
413 * or if one is, report the highest priority
414 * in the process. There isn't much more you can do as
415 * there is only room to return a single priority.
416 * Note: specifying our own pid is not the same
417 * as leaving it zero.
420 pri_to_rtp(td, &rtp);
424 rtp.type = RTP_PRIO_IDLE;
425 rtp.prio = RTP_PRIO_MAX;
426 FOREACH_THREAD_IN_PROC(p, tdp) {
427 pri_to_rtp(tdp, &rtp2);
428 if (rtp2.type < rtp.type ||
429 (rtp2.type == rtp.type &&
430 rtp2.prio < rtp.prio)) {
431 rtp.type = rtp2.type;
432 rtp.prio = rtp2.prio;
437 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
439 if ((error = p_cansched(td, p)) || (error = cierror))
443 * Disallow setting rtprio in most cases if not superuser.
444 * See the comment in sys_rtprio_thread about idprio
445 * threads holding a lock.
447 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME ||
448 (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
449 !unprivileged_idprio)) {
450 error = priv_check(td, PRIV_SCHED_RTPRIO);
456 * If we are setting our own priority, set just our
457 * thread but if we are doing another process,
458 * do all the threads on that process. If we
459 * specify our own pid we do the latter.
462 error = rtp_to_pri(&rtp, td);
464 FOREACH_THREAD_IN_PROC(p, td) {
465 if ((error = rtp_to_pri(&rtp, td)) != 0)
479 rtp_to_pri(struct rtprio *rtp, struct thread *td)
484 switch (RTP_PRIO_BASE(rtp->type)) {
485 case RTP_PRIO_REALTIME:
486 if (rtp->prio > RTP_PRIO_MAX)
488 newpri = PRI_MIN_REALTIME + rtp->prio;
490 case RTP_PRIO_NORMAL:
491 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
493 newpri = PRI_MIN_TIMESHARE + rtp->prio;
496 if (rtp->prio > RTP_PRIO_MAX)
498 newpri = PRI_MIN_IDLE + rtp->prio;
505 sched_class(td, rtp->type); /* XXX fix */
506 oldpri = td->td_user_pri;
507 sched_user_prio(td, newpri);
508 if (td->td_user_pri != oldpri && (td == curthread ||
509 td->td_priority == oldpri || td->td_user_pri <= PRI_MAX_REALTIME))
510 sched_prio(td, td->td_user_pri);
511 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
513 umtx_pi_adjust(td, oldpri);
520 pri_to_rtp(struct thread *td, struct rtprio *rtp)
524 switch (PRI_BASE(td->td_pri_class)) {
526 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
529 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
532 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
537 rtp->type = td->td_pri_class;
541 #if defined(COMPAT_43)
542 #ifndef _SYS_SYSPROTO_H_
543 struct osetrlimit_args {
551 register struct osetrlimit_args *uap;
557 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
559 lim.rlim_cur = olim.rlim_cur;
560 lim.rlim_max = olim.rlim_max;
561 error = kern_setrlimit(td, uap->which, &lim);
565 #ifndef _SYS_SYSPROTO_H_
566 struct ogetrlimit_args {
574 register struct ogetrlimit_args *uap;
581 if (uap->which >= RLIM_NLIMITS)
585 lim_rlimit(p, uap->which, &rl);
589 * XXX would be more correct to convert only RLIM_INFINITY to the
590 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
591 * values. Most 64->32 and 32->16 conversions, including not
592 * unimportant ones of uids are even more broken than what we
593 * do here (they blindly truncate). We don't do this correctly
594 * here since we have little experience with EOVERFLOW yet.
595 * Elsewhere, getuid() can't fail...
597 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
598 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
599 error = copyout(&olim, uap->rlp, sizeof(olim));
602 #endif /* COMPAT_43 */
604 #ifndef _SYS_SYSPROTO_H_
605 struct __setrlimit_args {
613 register struct __setrlimit_args *uap;
618 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
620 error = kern_setrlimit(td, uap->which, &alim);
632 PROC_LOCK_ASSERT(p, MA_OWNED);
634 * Check if the process exceeds its cpu resource allocation. If
635 * it reaches the max, arrange to kill the process in ast().
637 if (p->p_cpulimit == RLIM_INFINITY)
640 FOREACH_THREAD_IN_PROC(p, td) {
644 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
645 lim_rlimit(p, RLIMIT_CPU, &rlim);
646 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
647 killproc(p, "exceeded maximum CPU limit");
649 if (p->p_cpulimit < rlim.rlim_max)
654 if ((p->p_flag & P_WEXIT) == 0)
655 callout_reset(&p->p_limco, hz, lim_cb, p);
659 kern_setrlimit(td, which, limp)
664 struct plimit *newlim, *oldlim;
666 register struct rlimit *alimp;
667 struct rlimit oldssiz;
670 if (which >= RLIM_NLIMITS)
674 * Preserve historical bugs by treating negative limits as unsigned.
676 if (limp->rlim_cur < 0)
677 limp->rlim_cur = RLIM_INFINITY;
678 if (limp->rlim_max < 0)
679 limp->rlim_max = RLIM_INFINITY;
681 oldssiz.rlim_cur = 0;
683 newlim = lim_alloc();
686 alimp = &oldlim->pl_rlimit[which];
687 if (limp->rlim_cur > alimp->rlim_max ||
688 limp->rlim_max > alimp->rlim_max)
689 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
694 if (limp->rlim_cur > limp->rlim_max)
695 limp->rlim_cur = limp->rlim_max;
696 lim_copy(newlim, oldlim);
697 alimp = &newlim->pl_rlimit[which];
702 if (limp->rlim_cur != RLIM_INFINITY &&
703 p->p_cpulimit == RLIM_INFINITY)
704 callout_reset(&p->p_limco, hz, lim_cb, p);
705 p->p_cpulimit = limp->rlim_cur;
708 if (limp->rlim_cur > maxdsiz)
709 limp->rlim_cur = maxdsiz;
710 if (limp->rlim_max > maxdsiz)
711 limp->rlim_max = maxdsiz;
715 if (limp->rlim_cur > maxssiz)
716 limp->rlim_cur = maxssiz;
717 if (limp->rlim_max > maxssiz)
718 limp->rlim_max = maxssiz;
720 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
721 td->td_proc->p_sysent->sv_fixlimit(&oldssiz,
726 if (limp->rlim_cur > maxfilesperproc)
727 limp->rlim_cur = maxfilesperproc;
728 if (limp->rlim_max > maxfilesperproc)
729 limp->rlim_max = maxfilesperproc;
733 if (limp->rlim_cur > maxprocperuid)
734 limp->rlim_cur = maxprocperuid;
735 if (limp->rlim_max > maxprocperuid)
736 limp->rlim_max = maxprocperuid;
737 if (limp->rlim_cur < 1)
739 if (limp->rlim_max < 1)
743 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
744 td->td_proc->p_sysent->sv_fixlimit(limp, which);
750 if (which == RLIMIT_STACK) {
752 * Stack is allocated to the max at exec time with only
753 * "rlim_cur" bytes accessible. If stack limit is going
754 * up make more accessible, if going down make inaccessible.
756 if (limp->rlim_cur != oldssiz.rlim_cur) {
761 if (limp->rlim_cur > oldssiz.rlim_cur) {
762 prot = p->p_sysent->sv_stackprot;
763 size = limp->rlim_cur - oldssiz.rlim_cur;
764 addr = p->p_sysent->sv_usrstack -
768 size = oldssiz.rlim_cur - limp->rlim_cur;
769 addr = p->p_sysent->sv_usrstack -
772 addr = trunc_page(addr);
773 size = round_page(size);
774 (void)vm_map_protect(&p->p_vmspace->vm_map,
775 addr, addr + size, prot, FALSE);
782 #ifndef _SYS_SYSPROTO_H_
783 struct __getrlimit_args {
792 register struct __getrlimit_args *uap;
798 if (uap->which >= RLIM_NLIMITS)
802 lim_rlimit(p, uap->which, &rlim);
804 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
809 * Transform the running time and tick information for children of proc p
810 * into user and system time usage.
819 PROC_LOCK_ASSERT(p, MA_OWNED);
820 calcru1(p, &p->p_crux, up, sp);
824 * Transform the running time and tick information in proc p into user
825 * and system time usage. If appropriate, include the current time slice
829 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
834 PROC_LOCK_ASSERT(p, MA_OWNED);
835 PROC_SLOCK_ASSERT(p, MA_OWNED);
837 * If we are getting stats for the current process, then add in the
838 * stats that this thread has accumulated in its current time slice.
839 * We reset the thread and CPU state as if we had performed a context
843 if (td->td_proc == p) {
845 p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
846 PCPU_SET(switchtime, u);
848 /* Make sure the per-thread stats are current. */
849 FOREACH_THREAD_IN_PROC(p, td) {
850 if (td->td_incruntime == 0)
854 calcru1(p, &p->p_rux, up, sp);
858 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
861 /* {user, system, interrupt, total} {ticks, usec}: */
862 u_int64_t ut, uu, st, su, it, tt, tu;
864 ut = ruxp->rux_uticks;
865 st = ruxp->rux_sticks;
866 it = ruxp->rux_iticks;
869 /* Avoid divide by zero */
873 tu = cputick2usec(ruxp->rux_runtime);
874 if ((int64_t)tu < 0) {
875 /* XXX: this should be an assert /phk */
876 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
877 (intmax_t)tu, p->p_pid, p->p_comm);
881 if (tu >= ruxp->rux_tu) {
883 * The normal case, time increased.
884 * Enforce monotonicity of bucketed numbers.
887 if (uu < ruxp->rux_uu)
890 if (su < ruxp->rux_su)
892 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
894 * When we calibrate the cputicker, it is not uncommon to
895 * see the presumably fixed frequency increase slightly over
896 * time as a result of thermal stabilization and NTP
897 * discipline (of the reference clock). We therefore ignore
898 * a bit of backwards slop because we expect to catch up
899 * shortly. We use a 3 microsecond limit to catch low
900 * counts and a 1% limit for high counts.
905 } else { /* tu < ruxp->rux_tu */
907 * What happened here was likely that a laptop, which ran at
908 * a reduced clock frequency at boot, kicked into high gear.
909 * The wisdom of spamming this message in that case is
910 * dubious, but it might also be indicative of something
911 * serious, so lets keep it and hope laptops can be made
912 * more truthful about their CPU speed via ACPI.
914 printf("calcru: runtime went backwards from %ju usec "
915 "to %ju usec for pid %d (%s)\n",
916 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
917 p->p_pid, p->p_comm);
926 up->tv_sec = uu / 1000000;
927 up->tv_usec = uu % 1000000;
928 sp->tv_sec = su / 1000000;
929 sp->tv_usec = su % 1000000;
932 #ifndef _SYS_SYSPROTO_H_
933 struct getrusage_args {
935 struct rusage *rusage;
940 register struct thread *td;
941 register struct getrusage_args *uap;
946 error = kern_getrusage(td, uap->who, &ru);
948 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
953 kern_getrusage(struct thread *td, int who, struct rusage *rup)
963 rufetchcalc(p, rup, &rup->ru_utime,
967 case RUSAGE_CHILDREN:
968 *rup = p->p_stats->p_cru;
969 calccru(p, &rup->ru_utime, &rup->ru_stime);
978 calcru1(p, &td->td_rux, &rup->ru_utime, &rup->ru_stime);
990 rucollect(struct rusage *ru, struct rusage *ru2)
995 if (ru->ru_maxrss < ru2->ru_maxrss)
996 ru->ru_maxrss = ru2->ru_maxrss;
998 ip2 = &ru2->ru_first;
999 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1004 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1005 struct rusage_ext *rux2)
1008 rux->rux_runtime += rux2->rux_runtime;
1009 rux->rux_uticks += rux2->rux_uticks;
1010 rux->rux_sticks += rux2->rux_sticks;
1011 rux->rux_iticks += rux2->rux_iticks;
1012 rux->rux_uu += rux2->rux_uu;
1013 rux->rux_su += rux2->rux_su;
1014 rux->rux_tu += rux2->rux_tu;
1019 * Aggregate tick counts into the proc's rusage_ext.
1022 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1025 THREAD_LOCK_ASSERT(td, MA_OWNED);
1026 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1027 rux->rux_runtime += td->td_incruntime;
1028 rux->rux_uticks += td->td_uticks;
1029 rux->rux_sticks += td->td_sticks;
1030 rux->rux_iticks += td->td_iticks;
1034 ruxagg(struct proc *p, struct thread *td)
1038 ruxagg_locked(&p->p_rux, td);
1039 ruxagg_locked(&td->td_rux, td);
1040 td->td_incruntime = 0;
1048 * Update the rusage_ext structure and fetch a valid aggregate rusage
1049 * for proc p if storage for one is supplied.
1052 rufetch(struct proc *p, struct rusage *ru)
1056 PROC_SLOCK_ASSERT(p, MA_OWNED);
1059 if (p->p_numthreads > 0) {
1060 FOREACH_THREAD_IN_PROC(p, td) {
1062 rucollect(ru, &td->td_ru);
1068 * Atomically perform a rufetch and a calcru together.
1069 * Consumers, can safely assume the calcru is executed only once
1070 * rufetch is completed.
1073 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1084 * Allocate a new resource limits structure and initialize its
1085 * reference count and mutex pointer.
1090 struct plimit *limp;
1092 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1093 refcount_init(&limp->pl_refcnt, 1);
1099 struct plimit *limp;
1102 refcount_acquire(&limp->pl_refcnt);
1107 lim_fork(struct proc *p1, struct proc *p2)
1109 p2->p_limit = lim_hold(p1->p_limit);
1110 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1111 if (p1->p_cpulimit != RLIM_INFINITY)
1112 callout_reset(&p2->p_limco, hz, lim_cb, p2);
1117 struct plimit *limp;
1120 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1121 if (refcount_release(&limp->pl_refcnt))
1122 free((void *)limp, M_PLIMIT);
1126 * Make a copy of the plimit structure.
1127 * We share these structures copy-on-write after fork.
1131 struct plimit *dst, *src;
1134 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1135 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1139 * Return the hard limit for a particular system resource. The
1140 * which parameter specifies the index into the rlimit array.
1143 lim_max(struct proc *p, int which)
1147 lim_rlimit(p, which, &rl);
1148 return (rl.rlim_max);
1152 * Return the current (soft) limit for a particular system resource.
1153 * The which parameter which specifies the index into the rlimit array
1156 lim_cur(struct proc *p, int which)
1160 lim_rlimit(p, which, &rl);
1161 return (rl.rlim_cur);
1165 * Return a copy of the entire rlimit structure for the system limit
1166 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1169 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1172 PROC_LOCK_ASSERT(p, MA_OWNED);
1173 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1174 ("request for invalid resource limit"));
1175 *rlp = p->p_limit->pl_rlimit[which];
1176 if (p->p_sysent->sv_fixlimit != NULL)
1177 p->p_sysent->sv_fixlimit(rlp, which);
1181 * Find the uidinfo structure for a uid. This structure is used to
1182 * track the total resource consumption (process count, socket buffer
1183 * size, etc.) for the uid and impose limits.
1189 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1190 rw_init(&uihashtbl_lock, "uidinfo hash");
1194 * Look up a uidinfo struct for the parameter uid.
1195 * uihashtbl_lock must be locked.
1197 static struct uidinfo *
1201 struct uihashhead *uipp;
1202 struct uidinfo *uip;
1204 rw_assert(&uihashtbl_lock, RA_LOCKED);
1206 LIST_FOREACH(uip, uipp, ui_hash)
1207 if (uip->ui_uid == uid)
1214 * Find or allocate a struct uidinfo for a particular uid.
1215 * Increase refcount on uidinfo struct returned.
1216 * uifree() should be called on a struct uidinfo when released.
1222 struct uidinfo *old_uip, *uip;
1224 rw_rlock(&uihashtbl_lock);
1225 uip = uilookup(uid);
1227 rw_runlock(&uihashtbl_lock);
1228 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1229 rw_wlock(&uihashtbl_lock);
1231 * There's a chance someone created our uidinfo while we
1232 * were in malloc and not holding the lock, so we have to
1233 * make sure we don't insert a duplicate uidinfo.
1235 if ((old_uip = uilookup(uid)) != NULL) {
1236 /* Someone else beat us to it. */
1237 free(uip, M_UIDINFO);
1240 refcount_init(&uip->ui_ref, 0);
1242 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1244 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1248 rw_unlock(&uihashtbl_lock);
1253 * Place another refcount on a uidinfo struct.
1257 struct uidinfo *uip;
1260 refcount_acquire(&uip->ui_ref);
1264 * Since uidinfo structs have a long lifetime, we use an
1265 * opportunistic refcounting scheme to avoid locking the lookup hash
1268 * If the refcount hits 0, we need to free the structure,
1269 * which means we need to lock the hash.
1271 * After locking the struct and lowering the refcount, if we find
1272 * that we don't need to free, simply unlock and return.
1274 * If refcount lowering results in need to free, bump the count
1275 * back up, lose the lock and acquire the locks in the proper
1276 * order to try again.
1280 struct uidinfo *uip;
1284 /* Prepare for optimal case. */
1286 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1289 /* Prepare for suboptimal case. */
1290 rw_wlock(&uihashtbl_lock);
1291 if (refcount_release(&uip->ui_ref)) {
1292 LIST_REMOVE(uip, ui_hash);
1293 rw_wunlock(&uihashtbl_lock);
1294 if (uip->ui_sbsize != 0)
1295 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1296 uip->ui_uid, uip->ui_sbsize);
1297 if (uip->ui_proccnt != 0)
1298 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1299 uip->ui_uid, uip->ui_proccnt);
1300 if (uip->ui_vmsize != 0)
1301 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1302 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1303 mtx_destroy(&uip->ui_vmsize_mtx);
1304 free(uip, M_UIDINFO);
1308 * Someone added a reference between atomic_cmpset_int() and
1309 * rw_wlock(&uihashtbl_lock).
1311 rw_wunlock(&uihashtbl_lock);
1315 * Change the count associated with number of processes
1316 * a given user is using. When 'max' is 0, don't enforce a limit
1319 chgproccnt(uip, diff, max)
1320 struct uidinfo *uip;
1325 /* Don't allow them to exceed max, but allow subtraction. */
1326 if (diff > 0 && max != 0) {
1327 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1328 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1332 atomic_add_long(&uip->ui_proccnt, (long)diff);
1333 if (uip->ui_proccnt < 0)
1334 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1340 * Change the total socket buffer size a user has used.
1343 chgsbsize(uip, hiwat, to, max)
1344 struct uidinfo *uip;
1353 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1354 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1358 atomic_add_long(&uip->ui_sbsize, (long)diff);
1359 if (uip->ui_sbsize < 0)
1360 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1367 * Change the count associated with number of pseudo-terminals
1368 * a given user is using. When 'max' is 0, don't enforce a limit
1371 chgptscnt(uip, diff, max)
1372 struct uidinfo *uip;
1377 /* Don't allow them to exceed max, but allow subtraction. */
1378 if (diff > 0 && max != 0) {
1379 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1380 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1384 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1385 if (uip->ui_ptscnt < 0)
1386 printf("negative ptscnt for uid = %d\n", uip->ui_uid);