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/sysctl.h>
60 #include <sys/sysent.h>
65 #include <vm/vm_param.h>
67 #include <vm/vm_map.h>
70 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
71 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
72 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
73 static struct rwlock uihashtbl_lock;
74 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
75 static u_long uihash; /* size of hash table - 1 */
77 static void calcru1(struct proc *p, struct rusage_ext *ruxp,
78 struct timeval *up, struct timeval *sp);
79 static int donice(struct thread *td, struct proc *chgp, int n);
80 static struct uidinfo *uilookup(uid_t uid);
81 static void ruxagg_locked(struct rusage_ext *rux, struct thread *td);
83 static __inline int lim_shared(struct plimit *limp);
86 * Resource controls and accounting.
88 #ifndef _SYS_SYSPROTO_H_
89 struct getpriority_args {
95 sys_getpriority(td, uap)
97 register struct getpriority_args *uap;
105 switch (uap->which) {
109 low = td->td_proc->p_nice;
114 if (p_cansee(td, p) == 0)
121 sx_slock(&proctree_lock);
123 pg = td->td_proc->p_pgrp;
126 pg = pgfind(uap->who);
128 sx_sunlock(&proctree_lock);
132 sx_sunlock(&proctree_lock);
133 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
135 if (p->p_state == PRS_NORMAL &&
136 p_cansee(td, p) == 0) {
147 uap->who = td->td_ucred->cr_uid;
148 sx_slock(&allproc_lock);
149 FOREACH_PROC_IN_SYSTEM(p) {
151 if (p->p_state == PRS_NORMAL &&
152 p_cansee(td, p) == 0 &&
153 p->p_ucred->cr_uid == uap->who) {
159 sx_sunlock(&allproc_lock);
166 if (low == PRIO_MAX + 1 && error == 0)
168 td->td_retval[0] = low;
172 #ifndef _SYS_SYSPROTO_H_
173 struct setpriority_args {
180 sys_setpriority(td, uap)
182 struct setpriority_args *uap;
184 struct proc *curp, *p;
186 int found = 0, error = 0;
189 switch (uap->which) {
193 error = donice(td, curp, uap->prio);
199 error = p_cansee(td, p);
201 error = donice(td, p, uap->prio);
208 sx_slock(&proctree_lock);
213 pg = pgfind(uap->who);
215 sx_sunlock(&proctree_lock);
219 sx_sunlock(&proctree_lock);
220 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
222 if (p->p_state == PRS_NORMAL &&
223 p_cansee(td, p) == 0) {
224 error = donice(td, p, uap->prio);
234 uap->who = td->td_ucred->cr_uid;
235 sx_slock(&allproc_lock);
236 FOREACH_PROC_IN_SYSTEM(p) {
238 if (p->p_state == PRS_NORMAL &&
239 p->p_ucred->cr_uid == uap->who &&
240 p_cansee(td, p) == 0) {
241 error = donice(td, p, uap->prio);
246 sx_sunlock(&allproc_lock);
253 if (found == 0 && error == 0)
259 * Set "nice" for a (whole) process.
262 donice(struct thread *td, struct proc *p, int n)
266 PROC_LOCK_ASSERT(p, MA_OWNED);
267 if ((error = p_cansched(td, p)))
273 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
279 static int unprivileged_idprio;
280 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_idprio, CTLFLAG_RW,
281 &unprivileged_idprio, 0, "Allow non-root users to set an idle priority");
284 * Set realtime priority for LWP.
286 #ifndef _SYS_SYSPROTO_H_
287 struct rtprio_thread_args {
294 sys_rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
301 /* Perform copyin before acquiring locks if needed. */
302 if (uap->function == RTP_SET)
303 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
307 if (uap->lwpid == 0 || uap->lwpid == td->td_tid) {
312 /* Only look up thread in current process */
313 td1 = tdfind(uap->lwpid, curproc->p_pid);
319 switch (uap->function) {
321 if ((error = p_cansee(td, p)))
323 pri_to_rtp(td1, &rtp);
325 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
327 if ((error = p_cansched(td, p)) || (error = cierror))
330 /* Disallow setting rtprio in most cases if not superuser. */
333 * Realtime priority has to be restricted for reasons which
334 * should be obvious. However, for idleprio processes, there is
335 * a potential for system deadlock if an idleprio process gains
336 * a lock on a resource that other processes need (and the
337 * idleprio process can't run due to a CPU-bound normal
338 * process). Fix me! XXX
340 * This problem is not only related to idleprio process.
341 * A user level program can obtain a file lock and hold it
342 * indefinitely. Additionally, without idleprio processes it is
343 * still conceivable that a program with low priority will never
344 * get to run. In short, allowing this feature might make it
345 * easier to lock a resource indefinitely, but it is not the
346 * only thing that makes it possible.
348 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME ||
349 (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
350 unprivileged_idprio == 0)) {
351 error = priv_check(td, PRIV_SCHED_RTPRIO);
355 error = rtp_to_pri(&rtp, td1);
366 * Set realtime priority.
368 #ifndef _SYS_SYSPROTO_H_
377 struct thread *td; /* curthread */
378 register struct rtprio_args *uap;
385 /* Perform copyin before acquiring locks if needed. */
386 if (uap->function == RTP_SET)
387 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
400 switch (uap->function) {
402 if ((error = p_cansee(td, p)))
405 * Return OUR priority if no pid specified,
406 * or if one is, report the highest priority
407 * in the process. There isn't much more you can do as
408 * there is only room to return a single priority.
409 * Note: specifying our own pid is not the same
410 * as leaving it zero.
413 pri_to_rtp(td, &rtp);
417 rtp.type = RTP_PRIO_IDLE;
418 rtp.prio = RTP_PRIO_MAX;
419 FOREACH_THREAD_IN_PROC(p, tdp) {
420 pri_to_rtp(tdp, &rtp2);
421 if (rtp2.type < rtp.type ||
422 (rtp2.type == rtp.type &&
423 rtp2.prio < rtp.prio)) {
424 rtp.type = rtp2.type;
425 rtp.prio = rtp2.prio;
430 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
432 if ((error = p_cansched(td, p)) || (error = cierror))
436 * Disallow setting rtprio in most cases if not superuser.
437 * See the comment in sys_rtprio_thread about idprio
438 * threads holding a lock.
440 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME ||
441 (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
442 !unprivileged_idprio)) {
443 error = priv_check(td, PRIV_SCHED_RTPRIO);
449 * If we are setting our own priority, set just our
450 * thread but if we are doing another process,
451 * do all the threads on that process. If we
452 * specify our own pid we do the latter.
455 error = rtp_to_pri(&rtp, td);
457 FOREACH_THREAD_IN_PROC(p, td) {
458 if ((error = rtp_to_pri(&rtp, td)) != 0)
472 rtp_to_pri(struct rtprio *rtp, struct thread *td)
474 u_char newpri, oldclass, oldpri;
476 switch (RTP_PRIO_BASE(rtp->type)) {
477 case RTP_PRIO_REALTIME:
478 if (rtp->prio > RTP_PRIO_MAX)
480 newpri = PRI_MIN_REALTIME + rtp->prio;
482 case RTP_PRIO_NORMAL:
483 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
485 newpri = PRI_MIN_TIMESHARE + rtp->prio;
488 if (rtp->prio > RTP_PRIO_MAX)
490 newpri = PRI_MIN_IDLE + rtp->prio;
497 oldclass = td->td_pri_class;
498 sched_class(td, rtp->type); /* XXX fix */
499 oldpri = td->td_user_pri;
500 sched_user_prio(td, newpri);
501 if (td->td_user_pri != oldpri && (oldclass != RTP_PRIO_NORMAL ||
502 td->td_pri_class != RTP_PRIO_NORMAL))
503 sched_prio(td, td->td_user_pri);
504 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
507 umtx_pi_adjust(td, oldpri);
515 pri_to_rtp(struct thread *td, struct rtprio *rtp)
519 switch (PRI_BASE(td->td_pri_class)) {
521 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
524 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
527 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
532 rtp->type = td->td_pri_class;
536 #if defined(COMPAT_43)
537 #ifndef _SYS_SYSPROTO_H_
538 struct osetrlimit_args {
546 register struct osetrlimit_args *uap;
552 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
554 lim.rlim_cur = olim.rlim_cur;
555 lim.rlim_max = olim.rlim_max;
556 error = kern_setrlimit(td, uap->which, &lim);
560 #ifndef _SYS_SYSPROTO_H_
561 struct ogetrlimit_args {
569 register struct ogetrlimit_args *uap;
576 if (uap->which >= RLIM_NLIMITS)
580 lim_rlimit(p, uap->which, &rl);
584 * XXX would be more correct to convert only RLIM_INFINITY to the
585 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
586 * values. Most 64->32 and 32->16 conversions, including not
587 * unimportant ones of uids are even more broken than what we
588 * do here (they blindly truncate). We don't do this correctly
589 * here since we have little experience with EOVERFLOW yet.
590 * Elsewhere, getuid() can't fail...
592 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
593 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
594 error = copyout(&olim, uap->rlp, sizeof(olim));
597 #endif /* COMPAT_43 */
599 #ifndef _SYS_SYSPROTO_H_
600 struct __setrlimit_args {
606 sys_setrlimit(td, uap)
608 register struct __setrlimit_args *uap;
613 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
615 error = kern_setrlimit(td, uap->which, &alim);
627 PROC_LOCK_ASSERT(p, MA_OWNED);
629 * Check if the process exceeds its cpu resource allocation. If
630 * it reaches the max, arrange to kill the process in ast().
632 if (p->p_cpulimit == RLIM_INFINITY)
635 FOREACH_THREAD_IN_PROC(p, td) {
639 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
640 lim_rlimit(p, RLIMIT_CPU, &rlim);
641 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
642 killproc(p, "exceeded maximum CPU limit");
644 if (p->p_cpulimit < rlim.rlim_max)
646 kern_psignal(p, SIGXCPU);
649 if ((p->p_flag & P_WEXIT) == 0)
650 callout_reset_sbt(&p->p_limco, SBT_1S, 0,
651 lim_cb, p, C_PREL(1));
655 kern_setrlimit(struct thread *td, u_int which, struct rlimit *limp)
658 return (kern_proc_setrlimit(td, td->td_proc, which, limp));
662 kern_proc_setrlimit(struct thread *td, struct proc *p, u_int which,
665 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;
684 if (lim_shared(p->p_limit)) {
686 newlim = lim_alloc();
690 alimp = &oldlim->pl_rlimit[which];
691 if (limp->rlim_cur > alimp->rlim_max ||
692 limp->rlim_max > alimp->rlim_max)
693 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
699 if (limp->rlim_cur > limp->rlim_max)
700 limp->rlim_cur = limp->rlim_max;
701 if (newlim != NULL) {
702 lim_copy(newlim, oldlim);
703 alimp = &newlim->pl_rlimit[which];
709 if (limp->rlim_cur != RLIM_INFINITY &&
710 p->p_cpulimit == RLIM_INFINITY)
711 callout_reset_sbt(&p->p_limco, SBT_1S, 0,
712 lim_cb, p, C_PREL(1));
713 p->p_cpulimit = limp->rlim_cur;
716 if (limp->rlim_cur > maxdsiz)
717 limp->rlim_cur = maxdsiz;
718 if (limp->rlim_max > maxdsiz)
719 limp->rlim_max = maxdsiz;
723 if (limp->rlim_cur > maxssiz)
724 limp->rlim_cur = maxssiz;
725 if (limp->rlim_max > maxssiz)
726 limp->rlim_max = maxssiz;
728 if (p->p_sysent->sv_fixlimit != NULL)
729 p->p_sysent->sv_fixlimit(&oldssiz,
734 if (limp->rlim_cur > maxfilesperproc)
735 limp->rlim_cur = maxfilesperproc;
736 if (limp->rlim_max > maxfilesperproc)
737 limp->rlim_max = maxfilesperproc;
741 if (limp->rlim_cur > maxprocperuid)
742 limp->rlim_cur = maxprocperuid;
743 if (limp->rlim_max > maxprocperuid)
744 limp->rlim_max = maxprocperuid;
745 if (limp->rlim_cur < 1)
747 if (limp->rlim_max < 1)
751 if (p->p_sysent->sv_fixlimit != NULL)
752 p->p_sysent->sv_fixlimit(limp, which);
760 if (which == RLIMIT_STACK &&
762 * Skip calls from exec_new_vmspace(), done when stack is
765 (td != curthread || (p->p_flag & P_INEXEC) == 0)) {
767 * Stack is allocated to the max at exec time with only
768 * "rlim_cur" bytes accessible. If stack limit is going
769 * up make more accessible, if going down make inaccessible.
771 if (limp->rlim_cur != oldssiz.rlim_cur) {
776 if (limp->rlim_cur > oldssiz.rlim_cur) {
777 prot = p->p_sysent->sv_stackprot;
778 size = limp->rlim_cur - oldssiz.rlim_cur;
779 addr = p->p_sysent->sv_usrstack -
783 size = oldssiz.rlim_cur - limp->rlim_cur;
784 addr = p->p_sysent->sv_usrstack -
787 addr = trunc_page(addr);
788 size = round_page(size);
789 (void)vm_map_protect(&p->p_vmspace->vm_map,
790 addr, addr + size, prot, FALSE);
797 #ifndef _SYS_SYSPROTO_H_
798 struct __getrlimit_args {
805 sys_getrlimit(td, uap)
807 register struct __getrlimit_args *uap;
813 if (uap->which >= RLIM_NLIMITS)
817 lim_rlimit(p, uap->which, &rlim);
819 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
824 * Transform the running time and tick information for children of proc p
825 * into user and system time usage.
834 PROC_LOCK_ASSERT(p, MA_OWNED);
835 calcru1(p, &p->p_crux, up, sp);
839 * Transform the running time and tick information in proc p into user
840 * and system time usage. If appropriate, include the current time slice
844 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
849 PROC_LOCK_ASSERT(p, MA_OWNED);
850 PROC_STATLOCK_ASSERT(p, MA_OWNED);
852 * If we are getting stats for the current process, then add in the
853 * stats that this thread has accumulated in its current time slice.
854 * We reset the thread and CPU state as if we had performed a context
858 if (td->td_proc == p) {
860 runtime = u - PCPU_GET(switchtime);
861 td->td_runtime += runtime;
862 td->td_incruntime += runtime;
863 PCPU_SET(switchtime, u);
865 /* Make sure the per-thread stats are current. */
866 FOREACH_THREAD_IN_PROC(p, td) {
867 if (td->td_incruntime == 0)
871 calcru1(p, &p->p_rux, up, sp);
874 /* Collect resource usage for a single thread. */
876 rufetchtd(struct thread *td, struct rusage *ru)
882 PROC_STATLOCK_ASSERT(p, MA_OWNED);
883 THREAD_LOCK_ASSERT(td, MA_OWNED);
885 * If we are getting stats for the current thread, then add in the
886 * stats that this thread has accumulated in its current time slice.
887 * We reset the thread and CPU state as if we had performed a context
890 if (td == curthread) {
892 runtime = u - PCPU_GET(switchtime);
893 td->td_runtime += runtime;
894 td->td_incruntime += runtime;
895 PCPU_SET(switchtime, u);
899 calcru1(p, &td->td_rux, &ru->ru_utime, &ru->ru_stime);
903 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
906 /* {user, system, interrupt, total} {ticks, usec}: */
907 uint64_t ut, uu, st, su, it, tt, tu;
909 ut = ruxp->rux_uticks;
910 st = ruxp->rux_sticks;
911 it = ruxp->rux_iticks;
914 /* Avoid divide by zero */
918 tu = cputick2usec(ruxp->rux_runtime);
919 if ((int64_t)tu < 0) {
920 /* XXX: this should be an assert /phk */
921 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
922 (intmax_t)tu, p->p_pid, p->p_comm);
926 if (tu >= ruxp->rux_tu) {
928 * The normal case, time increased.
929 * Enforce monotonicity of bucketed numbers.
932 if (uu < ruxp->rux_uu)
935 if (su < ruxp->rux_su)
937 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
939 * When we calibrate the cputicker, it is not uncommon to
940 * see the presumably fixed frequency increase slightly over
941 * time as a result of thermal stabilization and NTP
942 * discipline (of the reference clock). We therefore ignore
943 * a bit of backwards slop because we expect to catch up
944 * shortly. We use a 3 microsecond limit to catch low
945 * counts and a 1% limit for high counts.
950 } else { /* tu < ruxp->rux_tu */
952 * What happened here was likely that a laptop, which ran at
953 * a reduced clock frequency at boot, kicked into high gear.
954 * The wisdom of spamming this message in that case is
955 * dubious, but it might also be indicative of something
956 * serious, so lets keep it and hope laptops can be made
957 * more truthful about their CPU speed via ACPI.
959 printf("calcru: runtime went backwards from %ju usec "
960 "to %ju usec for pid %d (%s)\n",
961 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
962 p->p_pid, p->p_comm);
971 up->tv_sec = uu / 1000000;
972 up->tv_usec = uu % 1000000;
973 sp->tv_sec = su / 1000000;
974 sp->tv_usec = su % 1000000;
977 #ifndef _SYS_SYSPROTO_H_
978 struct getrusage_args {
980 struct rusage *rusage;
984 sys_getrusage(td, uap)
985 register struct thread *td;
986 register struct getrusage_args *uap;
991 error = kern_getrusage(td, uap->who, &ru);
993 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
998 kern_getrusage(struct thread *td, int who, struct rusage *rup)
1008 rufetchcalc(p, rup, &rup->ru_utime,
1012 case RUSAGE_CHILDREN:
1013 *rup = p->p_stats->p_cru;
1014 calccru(p, &rup->ru_utime, &rup->ru_stime);
1033 rucollect(struct rusage *ru, struct rusage *ru2)
1038 if (ru->ru_maxrss < ru2->ru_maxrss)
1039 ru->ru_maxrss = ru2->ru_maxrss;
1041 ip2 = &ru2->ru_first;
1042 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1047 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1048 struct rusage_ext *rux2)
1051 rux->rux_runtime += rux2->rux_runtime;
1052 rux->rux_uticks += rux2->rux_uticks;
1053 rux->rux_sticks += rux2->rux_sticks;
1054 rux->rux_iticks += rux2->rux_iticks;
1055 rux->rux_uu += rux2->rux_uu;
1056 rux->rux_su += rux2->rux_su;
1057 rux->rux_tu += rux2->rux_tu;
1062 * Aggregate tick counts into the proc's rusage_ext.
1065 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1068 THREAD_LOCK_ASSERT(td, MA_OWNED);
1069 PROC_STATLOCK_ASSERT(td->td_proc, MA_OWNED);
1070 rux->rux_runtime += td->td_incruntime;
1071 rux->rux_uticks += td->td_uticks;
1072 rux->rux_sticks += td->td_sticks;
1073 rux->rux_iticks += td->td_iticks;
1077 ruxagg(struct proc *p, struct thread *td)
1081 ruxagg_locked(&p->p_rux, td);
1082 ruxagg_locked(&td->td_rux, td);
1083 td->td_incruntime = 0;
1091 * Update the rusage_ext structure and fetch a valid aggregate rusage
1092 * for proc p if storage for one is supplied.
1095 rufetch(struct proc *p, struct rusage *ru)
1099 PROC_STATLOCK_ASSERT(p, MA_OWNED);
1102 if (p->p_numthreads > 0) {
1103 FOREACH_THREAD_IN_PROC(p, td) {
1105 rucollect(ru, &td->td_ru);
1111 * Atomically perform a rufetch and a calcru together.
1112 * Consumers, can safely assume the calcru is executed only once
1113 * rufetch is completed.
1116 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1127 * Allocate a new resource limits structure and initialize its
1128 * reference count and mutex pointer.
1133 struct plimit *limp;
1135 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1136 refcount_init(&limp->pl_refcnt, 1);
1142 struct plimit *limp;
1145 refcount_acquire(&limp->pl_refcnt);
1151 struct plimit *limp;
1154 return (limp->pl_refcnt > 1);
1158 lim_fork(struct proc *p1, struct proc *p2)
1161 PROC_LOCK_ASSERT(p1, MA_OWNED);
1162 PROC_LOCK_ASSERT(p2, MA_OWNED);
1164 p2->p_limit = lim_hold(p1->p_limit);
1165 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1166 if (p1->p_cpulimit != RLIM_INFINITY)
1167 callout_reset_sbt(&p2->p_limco, SBT_1S, 0,
1168 lim_cb, p2, C_PREL(1));
1173 struct plimit *limp;
1176 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1177 if (refcount_release(&limp->pl_refcnt))
1178 free((void *)limp, M_PLIMIT);
1182 * Make a copy of the plimit structure.
1183 * We share these structures copy-on-write after fork.
1187 struct plimit *dst, *src;
1190 KASSERT(!lim_shared(dst), ("lim_copy to shared limit"));
1191 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1195 * Return the hard limit for a particular system resource. The
1196 * which parameter specifies the index into the rlimit array.
1199 lim_max(struct proc *p, int which)
1203 lim_rlimit(p, which, &rl);
1204 return (rl.rlim_max);
1208 * Return the current (soft) limit for a particular system resource.
1209 * The which parameter which specifies the index into the rlimit array
1212 lim_cur(struct proc *p, int which)
1216 lim_rlimit(p, which, &rl);
1217 return (rl.rlim_cur);
1221 * Return a copy of the entire rlimit structure for the system limit
1222 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1225 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1228 PROC_LOCK_ASSERT(p, MA_OWNED);
1229 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1230 ("request for invalid resource limit"));
1231 *rlp = p->p_limit->pl_rlimit[which];
1232 if (p->p_sysent->sv_fixlimit != NULL)
1233 p->p_sysent->sv_fixlimit(rlp, which);
1240 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1241 rw_init(&uihashtbl_lock, "uidinfo hash");
1245 * Look up a uidinfo struct for the parameter uid.
1246 * uihashtbl_lock must be locked.
1248 static struct uidinfo *
1252 struct uihashhead *uipp;
1253 struct uidinfo *uip;
1255 rw_assert(&uihashtbl_lock, RA_LOCKED);
1257 LIST_FOREACH(uip, uipp, ui_hash)
1258 if (uip->ui_uid == uid)
1265 * Find or allocate a struct uidinfo for a particular uid.
1266 * Increase refcount on uidinfo struct returned.
1267 * uifree() should be called on a struct uidinfo when released.
1273 struct uidinfo *old_uip, *uip;
1275 rw_rlock(&uihashtbl_lock);
1276 uip = uilookup(uid);
1278 rw_runlock(&uihashtbl_lock);
1279 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1280 racct_create(&uip->ui_racct);
1281 rw_wlock(&uihashtbl_lock);
1283 * There's a chance someone created our uidinfo while we
1284 * were in malloc and not holding the lock, so we have to
1285 * make sure we don't insert a duplicate uidinfo.
1287 if ((old_uip = uilookup(uid)) != NULL) {
1288 /* Someone else beat us to it. */
1289 racct_destroy(&uip->ui_racct);
1290 free(uip, M_UIDINFO);
1293 refcount_init(&uip->ui_ref, 0);
1295 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1297 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1301 rw_unlock(&uihashtbl_lock);
1306 * Place another refcount on a uidinfo struct.
1310 struct uidinfo *uip;
1313 refcount_acquire(&uip->ui_ref);
1317 * Since uidinfo structs have a long lifetime, we use an
1318 * opportunistic refcounting scheme to avoid locking the lookup hash
1321 * If the refcount hits 0, we need to free the structure,
1322 * which means we need to lock the hash.
1324 * After locking the struct and lowering the refcount, if we find
1325 * that we don't need to free, simply unlock and return.
1327 * If refcount lowering results in need to free, bump the count
1328 * back up, lose the lock and acquire the locks in the proper
1329 * order to try again.
1333 struct uidinfo *uip;
1337 /* Prepare for optimal case. */
1339 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1342 /* Prepare for suboptimal case. */
1343 rw_wlock(&uihashtbl_lock);
1344 if (refcount_release(&uip->ui_ref)) {
1345 racct_destroy(&uip->ui_racct);
1346 LIST_REMOVE(uip, ui_hash);
1347 rw_wunlock(&uihashtbl_lock);
1348 if (uip->ui_sbsize != 0)
1349 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1350 uip->ui_uid, uip->ui_sbsize);
1351 if (uip->ui_proccnt != 0)
1352 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1353 uip->ui_uid, uip->ui_proccnt);
1354 if (uip->ui_vmsize != 0)
1355 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1356 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1357 mtx_destroy(&uip->ui_vmsize_mtx);
1358 free(uip, M_UIDINFO);
1362 * Someone added a reference between atomic_cmpset_int() and
1363 * rw_wlock(&uihashtbl_lock).
1365 rw_wunlock(&uihashtbl_lock);
1369 ui_racct_foreach(void (*callback)(struct racct *racct,
1370 void *arg2, void *arg3), void *arg2, void *arg3)
1372 struct uidinfo *uip;
1373 struct uihashhead *uih;
1375 rw_rlock(&uihashtbl_lock);
1376 for (uih = &uihashtbl[uihash]; uih >= uihashtbl; uih--) {
1377 LIST_FOREACH(uip, uih, ui_hash) {
1378 (callback)(uip->ui_racct, arg2, arg3);
1381 rw_runlock(&uihashtbl_lock);
1385 * Change the count associated with number of processes
1386 * a given user is using. When 'max' is 0, don't enforce a limit
1389 chgproccnt(uip, diff, max)
1390 struct uidinfo *uip;
1395 /* Don't allow them to exceed max, but allow subtraction. */
1396 if (diff > 0 && max != 0) {
1397 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1398 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1402 atomic_add_long(&uip->ui_proccnt, (long)diff);
1403 if (uip->ui_proccnt < 0)
1404 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1410 * Change the total socket buffer size a user has used.
1413 chgsbsize(uip, hiwat, to, max)
1414 struct uidinfo *uip;
1423 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1424 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1428 atomic_add_long(&uip->ui_sbsize, (long)diff);
1429 if (uip->ui_sbsize < 0)
1430 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1437 * Change the count associated with number of pseudo-terminals
1438 * a given user is using. When 'max' is 0, don't enforce a limit
1441 chgptscnt(uip, diff, max)
1442 struct uidinfo *uip;
1447 /* Don't allow them to exceed max, but allow subtraction. */
1448 if (diff > 0 && max != 0) {
1449 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1450 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1454 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1455 if (uip->ui_ptscnt < 0)
1456 printf("negative ptscnt for uid = %d\n", uip->ui_uid);