2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/refcount.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysent.h>
62 #include <vm/vm_param.h>
64 #include <vm/vm_map.h>
67 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
68 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
69 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
70 static struct mtx uihashtbl_mtx;
71 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
72 static u_long uihash; /* size of hash table - 1 */
74 static void calcru1(struct proc *p, struct rusage_ext *ruxp,
75 struct timeval *up, struct timeval *sp);
76 static int donice(struct thread *td, struct proc *chgp, int n);
77 static struct uidinfo *uilookup(uid_t uid);
80 * Resource controls and accounting.
82 #ifndef _SYS_SYSPROTO_H_
83 struct getpriority_args {
91 register struct getpriority_args *uap;
103 low = td->td_proc->p_nice;
108 if (p_cansee(td, p) == 0)
115 sx_slock(&proctree_lock);
117 pg = td->td_proc->p_pgrp;
120 pg = pgfind(uap->who);
122 sx_sunlock(&proctree_lock);
126 sx_sunlock(&proctree_lock);
127 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
129 if (!p_cansee(td, p)) {
140 uap->who = td->td_ucred->cr_uid;
141 sx_slock(&allproc_lock);
142 FOREACH_PROC_IN_SYSTEM(p) {
143 /* Do not bother to check PRS_NEW processes */
144 if (p->p_state == PRS_NEW)
147 if (!p_cansee(td, p) &&
148 p->p_ucred->cr_uid == uap->who) {
154 sx_sunlock(&allproc_lock);
161 if (low == PRIO_MAX + 1 && error == 0)
163 td->td_retval[0] = low;
167 #ifndef _SYS_SYSPROTO_H_
168 struct setpriority_args {
177 struct setpriority_args *uap;
179 struct proc *curp, *p;
181 int found = 0, error = 0;
184 switch (uap->which) {
188 error = donice(td, curp, uap->prio);
194 if (p_cansee(td, p) == 0)
195 error = donice(td, p, uap->prio);
202 sx_slock(&proctree_lock);
207 pg = pgfind(uap->who);
209 sx_sunlock(&proctree_lock);
213 sx_sunlock(&proctree_lock);
214 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
216 if (!p_cansee(td, p)) {
217 error = donice(td, p, uap->prio);
227 uap->who = td->td_ucred->cr_uid;
228 sx_slock(&allproc_lock);
229 FOREACH_PROC_IN_SYSTEM(p) {
231 if (p->p_ucred->cr_uid == uap->who &&
233 error = donice(td, p, uap->prio);
238 sx_sunlock(&allproc_lock);
245 if (found == 0 && error == 0)
251 * Set "nice" for a (whole) process.
254 donice(struct thread *td, struct proc *p, int n)
258 PROC_LOCK_ASSERT(p, MA_OWNED);
259 if ((error = p_cansched(td, p)))
265 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
274 * Set realtime priority for LWP.
276 #ifndef _SYS_SYSPROTO_H_
277 struct rtprio_thread_args {
284 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));
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)))
311 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
314 td1 = thread_find(p, uap->lwpid);
316 pri_to_rtp(td1, &rtp);
321 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
323 if ((error = p_cansched(td, p)) || (error = cierror))
326 /* Disallow setting rtprio in most cases if not superuser. */
328 * Realtime priority has to be restricted for reasons which should be
329 * obvious. However, for idle priority, there is a potential for
330 * system deadlock if an idleprio process gains a lock on a resource
331 * that other processes need (and the idleprio process can't run
332 * due to a CPU-bound normal process). Fix me! XXX
335 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
337 if (rtp.type != RTP_PRIO_NORMAL) {
339 error = priv_check(td, PRIV_SCHED_RTPRIO);
345 if (uap->lwpid == 0 || uap->lwpid == td->td_tid)
348 td1 = thread_find(p, uap->lwpid);
350 error = rtp_to_pri(&rtp, td1);
364 * Set realtime priority.
366 #ifndef _SYS_SYSPROTO_H_
375 struct thread *td; /* curthread */
376 register struct rtprio_args *uap;
384 /* Perform copyin before acquiring locks if needed. */
385 if (uap->function == RTP_SET)
386 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
400 switch (uap->function) {
402 if ((error = p_cansee(td, p)))
406 * Return OUR priority if no pid specified,
407 * or if one is, report the highest priority
408 * in the process. There isn't much more you can do as
409 * there is only room to return a single priority.
410 * XXXKSE: maybe need a new interface to report
411 * priorities of multiple system scope threads.
412 * Note: specifying our own pid is not the same
413 * as leaving it zero.
416 pri_to_rtp(td, &rtp);
420 rtp.type = RTP_PRIO_IDLE;
421 rtp.prio = RTP_PRIO_MAX;
422 FOREACH_THREAD_IN_PROC(p, tdp) {
423 pri_to_rtp(tdp, &rtp2);
424 if (rtp2.type < rtp.type ||
425 (rtp2.type == rtp.type &&
426 rtp2.prio < rtp.prio)) {
427 rtp.type = rtp2.type;
428 rtp.prio = rtp2.prio;
434 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
436 if ((error = p_cansched(td, p)) || (error = cierror))
439 /* Disallow setting rtprio in most cases if not superuser. */
441 * Realtime priority has to be restricted for reasons which should be
442 * obvious. However, for idle priority, there is a potential for
443 * system deadlock if an idleprio process gains a lock on a resource
444 * that other processes need (and the idleprio process can't run
445 * due to a CPU-bound normal process). Fix me! XXX
448 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
450 if (rtp.type != RTP_PRIO_NORMAL) {
452 error = priv_check(td, PRIV_SCHED_RTPRIO);
458 * If we are setting our own priority, set just our
459 * thread but if we are doing another process,
460 * do all the threads on that process. If we
461 * specify our own pid we do the latter.
465 error = rtp_to_pri(&rtp, td);
467 FOREACH_THREAD_IN_PROC(p, td) {
468 if ((error = rtp_to_pri(&rtp, td)) != 0)
483 rtp_to_pri(struct rtprio *rtp, struct thread *td)
488 if (rtp->prio > RTP_PRIO_MAX)
491 switch (RTP_PRIO_BASE(rtp->type)) {
492 case RTP_PRIO_REALTIME:
493 newpri = PRI_MIN_REALTIME + rtp->prio;
495 case RTP_PRIO_NORMAL:
496 newpri = PRI_MIN_TIMESHARE + rtp->prio;
499 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);
509 sched_prio(curthread, td->td_user_pri); /* XXX dubious */
510 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
512 umtx_pi_adjust(td, oldpri);
519 pri_to_rtp(struct thread *td, struct rtprio *rtp)
523 switch (PRI_BASE(td->td_pri_class)) {
525 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
528 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
531 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
536 rtp->type = td->td_pri_class;
540 #if defined(COMPAT_43)
541 #ifndef _SYS_SYSPROTO_H_
542 struct osetrlimit_args {
550 register struct osetrlimit_args *uap;
556 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
558 lim.rlim_cur = olim.rlim_cur;
559 lim.rlim_max = olim.rlim_max;
560 error = kern_setrlimit(td, uap->which, &lim);
564 #ifndef _SYS_SYSPROTO_H_
565 struct ogetrlimit_args {
573 register struct ogetrlimit_args *uap;
580 if (uap->which >= RLIM_NLIMITS)
584 lim_rlimit(p, uap->which, &rl);
588 * XXX would be more correct to convert only RLIM_INFINITY to the
589 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
590 * values. Most 64->32 and 32->16 conversions, including not
591 * unimportant ones of uids are even more broken than what we
592 * do here (they blindly truncate). We don't do this correctly
593 * here since we have little experience with EOVERFLOW yet.
594 * Elsewhere, getuid() can't fail...
596 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
597 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
598 error = copyout(&olim, uap->rlp, sizeof(olim));
601 #endif /* COMPAT_43 */
603 #ifndef _SYS_SYSPROTO_H_
604 struct __setrlimit_args {
612 register struct __setrlimit_args *uap;
617 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
619 error = kern_setrlimit(td, uap->which, &alim);
631 PROC_LOCK_ASSERT(p, MA_OWNED);
633 * Check if the process exceeds its cpu resource allocation. If
634 * it reaches the max, arrange to kill the process in ast().
636 if (p->p_cpulimit == RLIM_INFINITY)
639 FOREACH_THREAD_IN_PROC(p, td) {
641 ruxagg(&p->p_rux, td);
645 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
646 lim_rlimit(p, RLIMIT_CPU, &rlim);
647 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
648 killproc(p, "exceeded maximum CPU limit");
650 if (p->p_cpulimit < rlim.rlim_max)
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);
706 p->p_cpulimit = limp->rlim_cur;
710 if (limp->rlim_cur > maxdsiz)
711 limp->rlim_cur = maxdsiz;
712 if (limp->rlim_max > maxdsiz)
713 limp->rlim_max = maxdsiz;
717 if (limp->rlim_cur > maxssiz)
718 limp->rlim_cur = maxssiz;
719 if (limp->rlim_max > maxssiz)
720 limp->rlim_max = maxssiz;
722 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
723 td->td_proc->p_sysent->sv_fixlimit(&oldssiz,
728 if (limp->rlim_cur > maxfilesperproc)
729 limp->rlim_cur = maxfilesperproc;
730 if (limp->rlim_max > maxfilesperproc)
731 limp->rlim_max = maxfilesperproc;
735 if (limp->rlim_cur > maxprocperuid)
736 limp->rlim_cur = maxprocperuid;
737 if (limp->rlim_max > maxprocperuid)
738 limp->rlim_max = maxprocperuid;
739 if (limp->rlim_cur < 1)
741 if (limp->rlim_max < 1)
745 if (td->td_proc->p_sysent->sv_fixlimit != NULL)
746 td->td_proc->p_sysent->sv_fixlimit(limp, which);
752 if (which == RLIMIT_STACK) {
754 * Stack is allocated to the max at exec time with only
755 * "rlim_cur" bytes accessible. If stack limit is going
756 * up make more accessible, if going down make inaccessible.
758 if (limp->rlim_cur != oldssiz.rlim_cur) {
763 if (limp->rlim_cur > oldssiz.rlim_cur) {
764 prot = p->p_sysent->sv_stackprot;
765 size = limp->rlim_cur - oldssiz.rlim_cur;
766 addr = p->p_sysent->sv_usrstack -
770 size = oldssiz.rlim_cur - limp->rlim_cur;
771 addr = p->p_sysent->sv_usrstack -
774 addr = trunc_page(addr);
775 size = round_page(size);
776 (void)vm_map_protect(&p->p_vmspace->vm_map,
777 addr, addr + size, prot, FALSE);
784 #ifndef _SYS_SYSPROTO_H_
785 struct __getrlimit_args {
794 register struct __getrlimit_args *uap;
800 if (uap->which >= RLIM_NLIMITS)
804 lim_rlimit(p, uap->which, &rlim);
806 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
811 * Transform the running time and tick information for children of proc p
812 * into user and system time usage.
821 PROC_LOCK_ASSERT(p, MA_OWNED);
822 calcru1(p, &p->p_crux, up, sp);
826 * Transform the running time and tick information in proc p into user
827 * and system time usage. If appropriate, include the current time slice
831 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
836 PROC_LOCK_ASSERT(p, MA_OWNED);
837 PROC_SLOCK_ASSERT(p, MA_OWNED);
839 * If we are getting stats for the current process, then add in the
840 * stats that this thread has accumulated in its current time slice.
841 * We reset the thread and CPU state as if we had performed a context
845 if (td->td_proc == p) {
847 p->p_rux.rux_runtime += u - PCPU_GET(switchtime);
848 PCPU_SET(switchtime, u);
850 /* Make sure the per-thread stats are current. */
851 FOREACH_THREAD_IN_PROC(p, td) {
852 if (td->td_incruntime == 0)
855 ruxagg(&p->p_rux, td);
858 calcru1(p, &p->p_rux, up, sp);
862 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
865 /* {user, system, interrupt, total} {ticks, usec}: */
866 u_int64_t ut, uu, st, su, it, tt, tu;
868 ut = ruxp->rux_uticks;
869 st = ruxp->rux_sticks;
870 it = ruxp->rux_iticks;
873 /* Avoid divide by zero */
877 tu = cputick2usec(ruxp->rux_runtime);
878 if ((int64_t)tu < 0) {
879 /* XXX: this should be an assert /phk */
880 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
881 (intmax_t)tu, p->p_pid, p->p_comm);
885 if (tu >= ruxp->rux_tu) {
887 * The normal case, time increased.
888 * Enforce monotonicity of bucketed numbers.
891 if (uu < ruxp->rux_uu)
894 if (su < ruxp->rux_su)
896 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
898 * When we calibrate the cputicker, it is not uncommon to
899 * see the presumably fixed frequency increase slightly over
900 * time as a result of thermal stabilization and NTP
901 * discipline (of the reference clock). We therefore ignore
902 * a bit of backwards slop because we expect to catch up
903 * shortly. We use a 3 microsecond limit to catch low
904 * counts and a 1% limit for high counts.
909 } else { /* tu < ruxp->rux_tu */
911 * What happene here was likely that a laptop, which ran at
912 * a reduced clock frequency at boot, kicked into high gear.
913 * The wisdom of spamming this message in that case is
914 * dubious, but it might also be indicative of something
915 * serious, so lets keep it and hope laptops can be made
916 * more truthful about their CPU speed via ACPI.
918 printf("calcru: runtime went backwards from %ju usec "
919 "to %ju usec for pid %d (%s)\n",
920 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
921 p->p_pid, p->p_comm);
930 up->tv_sec = uu / 1000000;
931 up->tv_usec = uu % 1000000;
932 sp->tv_sec = su / 1000000;
933 sp->tv_usec = su % 1000000;
936 #ifndef _SYS_SYSPROTO_H_
937 struct getrusage_args {
939 struct rusage *rusage;
944 register struct thread *td;
945 register struct getrusage_args *uap;
950 error = kern_getrusage(td, uap->who, &ru);
952 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
957 kern_getrusage(td, who, rup)
969 rufetchcalc(p, rup, &rup->ru_utime,
973 case RUSAGE_CHILDREN:
974 *rup = p->p_stats->p_cru;
975 calccru(p, &rup->ru_utime, &rup->ru_stime);
987 rucollect(struct rusage *ru, struct rusage *ru2)
992 if (ru->ru_maxrss < ru2->ru_maxrss)
993 ru->ru_maxrss = ru2->ru_maxrss;
995 ip2 = &ru2->ru_first;
996 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1001 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1002 struct rusage_ext *rux2)
1005 rux->rux_runtime += rux2->rux_runtime;
1006 rux->rux_uticks += rux2->rux_uticks;
1007 rux->rux_sticks += rux2->rux_sticks;
1008 rux->rux_iticks += rux2->rux_iticks;
1009 rux->rux_uu += rux2->rux_uu;
1010 rux->rux_su += rux2->rux_su;
1011 rux->rux_tu += rux2->rux_tu;
1016 * Aggregate tick counts into the proc's rusage_ext.
1019 ruxagg(struct rusage_ext *rux, struct thread *td)
1022 THREAD_LOCK_ASSERT(td, MA_OWNED);
1023 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1024 rux->rux_runtime += td->td_incruntime;
1025 rux->rux_uticks += td->td_uticks;
1026 rux->rux_sticks += td->td_sticks;
1027 rux->rux_iticks += td->td_iticks;
1028 td->td_incruntime = 0;
1035 * Update the rusage_ext structure and fetch a valid aggregate rusage
1036 * for proc p if storage for one is supplied.
1039 rufetch(struct proc *p, struct rusage *ru)
1043 PROC_SLOCK_ASSERT(p, MA_OWNED);
1046 if (p->p_numthreads > 0) {
1047 FOREACH_THREAD_IN_PROC(p, td) {
1049 ruxagg(&p->p_rux, td);
1051 rucollect(ru, &td->td_ru);
1057 * Atomically perform a rufetch and a calcru together.
1058 * Consumers, can safely assume the calcru is executed only once
1059 * rufetch is completed.
1062 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1073 * Allocate a new resource limits structure and initialize its
1074 * reference count and mutex pointer.
1079 struct plimit *limp;
1081 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1082 refcount_init(&limp->pl_refcnt, 1);
1088 struct plimit *limp;
1091 refcount_acquire(&limp->pl_refcnt);
1096 lim_fork(struct proc *p1, struct proc *p2)
1098 p2->p_limit = lim_hold(p1->p_limit);
1099 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1100 if (p1->p_cpulimit != RLIM_INFINITY)
1101 callout_reset(&p2->p_limco, hz, lim_cb, p2);
1106 struct plimit *limp;
1109 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1110 if (refcount_release(&limp->pl_refcnt))
1111 free((void *)limp, M_PLIMIT);
1115 * Make a copy of the plimit structure.
1116 * We share these structures copy-on-write after fork.
1120 struct plimit *dst, *src;
1123 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1124 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1128 * Return the hard limit for a particular system resource. The
1129 * which parameter specifies the index into the rlimit array.
1132 lim_max(struct proc *p, int which)
1136 lim_rlimit(p, which, &rl);
1137 return (rl.rlim_max);
1141 * Return the current (soft) limit for a particular system resource.
1142 * The which parameter which specifies the index into the rlimit array
1145 lim_cur(struct proc *p, int which)
1149 lim_rlimit(p, which, &rl);
1150 return (rl.rlim_cur);
1154 * Return a copy of the entire rlimit structure for the system limit
1155 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1158 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1161 PROC_LOCK_ASSERT(p, MA_OWNED);
1162 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1163 ("request for invalid resource limit"));
1164 *rlp = p->p_limit->pl_rlimit[which];
1165 if (p->p_sysent->sv_fixlimit != NULL)
1166 p->p_sysent->sv_fixlimit(rlp, which);
1170 * Find the uidinfo structure for a uid. This structure is used to
1171 * track the total resource consumption (process count, socket buffer
1172 * size, etc.) for the uid and impose limits.
1178 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1179 mtx_init(&uihashtbl_mtx, "uidinfo hash", NULL, MTX_DEF);
1183 * Look up a uidinfo struct for the parameter uid.
1184 * uihashtbl_mtx must be locked.
1186 static struct uidinfo *
1190 struct uihashhead *uipp;
1191 struct uidinfo *uip;
1193 mtx_assert(&uihashtbl_mtx, MA_OWNED);
1195 LIST_FOREACH(uip, uipp, ui_hash)
1196 if (uip->ui_uid == uid)
1203 * Find or allocate a struct uidinfo for a particular uid.
1204 * Increase refcount on uidinfo struct returned.
1205 * uifree() should be called on a struct uidinfo when released.
1211 struct uidinfo *old_uip, *uip;
1213 mtx_lock(&uihashtbl_mtx);
1214 uip = uilookup(uid);
1216 mtx_unlock(&uihashtbl_mtx);
1217 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1218 mtx_lock(&uihashtbl_mtx);
1220 * There's a chance someone created our uidinfo while we
1221 * were in malloc and not holding the lock, so we have to
1222 * make sure we don't insert a duplicate uidinfo.
1224 if ((old_uip = uilookup(uid)) != NULL) {
1225 /* Someone else beat us to it. */
1226 free(uip, M_UIDINFO);
1229 uip->ui_mtxp = mtx_pool_alloc(mtxpool_sleep);
1231 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1235 mtx_unlock(&uihashtbl_mtx);
1240 * Place another refcount on a uidinfo struct.
1244 struct uidinfo *uip;
1249 UIDINFO_UNLOCK(uip);
1253 * Since uidinfo structs have a long lifetime, we use an
1254 * opportunistic refcounting scheme to avoid locking the lookup hash
1257 * If the refcount hits 0, we need to free the structure,
1258 * which means we need to lock the hash.
1260 * After locking the struct and lowering the refcount, if we find
1261 * that we don't need to free, simply unlock and return.
1263 * If refcount lowering results in need to free, bump the count
1264 * back up, lose the lock and acquire the locks in the proper
1265 * order to try again.
1269 struct uidinfo *uip;
1272 /* Prepare for optimal case. */
1275 if (--uip->ui_ref != 0) {
1276 UIDINFO_UNLOCK(uip);
1280 /* Prepare for suboptimal case. */
1282 UIDINFO_UNLOCK(uip);
1283 mtx_lock(&uihashtbl_mtx);
1287 * We must subtract one from the count again because we backed out
1288 * our initial subtraction before dropping the lock.
1289 * Since another thread may have added a reference after we dropped the
1290 * initial lock we have to test for zero again.
1292 if (--uip->ui_ref == 0) {
1293 LIST_REMOVE(uip, ui_hash);
1294 mtx_unlock(&uihashtbl_mtx);
1295 if (uip->ui_sbsize != 0)
1296 printf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1297 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1298 if (uip->ui_proccnt != 0)
1299 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1300 uip->ui_uid, uip->ui_proccnt);
1301 UIDINFO_UNLOCK(uip);
1302 FREE(uip, M_UIDINFO);
1306 mtx_unlock(&uihashtbl_mtx);
1307 UIDINFO_UNLOCK(uip);
1311 * Change the count associated with number of processes
1312 * a given user is using. When 'max' is 0, don't enforce a limit
1315 chgproccnt(uip, diff, max)
1316 struct uidinfo *uip;
1322 /* Don't allow them to exceed max, but allow subtraction. */
1323 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1324 UIDINFO_UNLOCK(uip);
1327 uip->ui_proccnt += diff;
1328 if (uip->ui_proccnt < 0)
1329 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1330 UIDINFO_UNLOCK(uip);
1335 * Change the total socket buffer size a user has used.
1338 chgsbsize(uip, hiwat, to, max)
1339 struct uidinfo *uip;
1347 new = uip->ui_sbsize + to - *hiwat;
1348 /* Don't allow them to exceed max, but allow subtraction. */
1349 if (to > *hiwat && new > max) {
1350 UIDINFO_UNLOCK(uip);
1353 uip->ui_sbsize = new;
1354 UIDINFO_UNLOCK(uip);
1357 printf("negative sbsize for uid = %d\n", uip->ui_uid);