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 * Stack is allocated to the max at exec time with only
763 * "rlim_cur" bytes accessible. If stack limit is going
764 * up make more accessible, if going down make inaccessible.
766 if (limp->rlim_cur != oldssiz.rlim_cur) {
771 if (limp->rlim_cur > oldssiz.rlim_cur) {
772 prot = p->p_sysent->sv_stackprot;
773 size = limp->rlim_cur - oldssiz.rlim_cur;
774 addr = p->p_sysent->sv_usrstack -
778 size = oldssiz.rlim_cur - limp->rlim_cur;
779 addr = p->p_sysent->sv_usrstack -
782 addr = trunc_page(addr);
783 size = round_page(size);
784 (void)vm_map_protect(&p->p_vmspace->vm_map,
785 addr, addr + size, prot, FALSE);
792 #ifndef _SYS_SYSPROTO_H_
793 struct __getrlimit_args {
800 sys_getrlimit(td, uap)
802 register struct __getrlimit_args *uap;
808 if (uap->which >= RLIM_NLIMITS)
812 lim_rlimit(p, uap->which, &rlim);
814 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
819 * Transform the running time and tick information for children of proc p
820 * into user and system time usage.
829 PROC_LOCK_ASSERT(p, MA_OWNED);
830 calcru1(p, &p->p_crux, up, sp);
834 * Transform the running time and tick information in proc p into user
835 * and system time usage. If appropriate, include the current time slice
839 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
844 PROC_LOCK_ASSERT(p, MA_OWNED);
845 PROC_SLOCK_ASSERT(p, MA_OWNED);
847 * If we are getting stats for the current process, then add in the
848 * stats that this thread has accumulated in its current time slice.
849 * We reset the thread and CPU state as if we had performed a context
853 if (td->td_proc == p) {
855 runtime = u - PCPU_GET(switchtime);
856 td->td_runtime += runtime;
857 td->td_incruntime += runtime;
858 PCPU_SET(switchtime, u);
860 /* Make sure the per-thread stats are current. */
861 FOREACH_THREAD_IN_PROC(p, td) {
862 if (td->td_incruntime == 0)
866 calcru1(p, &p->p_rux, up, sp);
869 /* Collect resource usage for a single thread. */
871 rufetchtd(struct thread *td, struct rusage *ru)
877 PROC_SLOCK_ASSERT(p, MA_OWNED);
878 THREAD_LOCK_ASSERT(td, MA_OWNED);
880 * If we are getting stats for the current thread, then add in the
881 * stats that this thread has accumulated in its current time slice.
882 * We reset the thread and CPU state as if we had performed a context
885 if (td == curthread) {
887 runtime = u - PCPU_GET(switchtime);
888 td->td_runtime += runtime;
889 td->td_incruntime += runtime;
890 PCPU_SET(switchtime, u);
894 calcru1(p, &td->td_rux, &ru->ru_utime, &ru->ru_stime);
898 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
901 /* {user, system, interrupt, total} {ticks, usec}: */
902 uint64_t ut, uu, st, su, it, tt, tu;
904 ut = ruxp->rux_uticks;
905 st = ruxp->rux_sticks;
906 it = ruxp->rux_iticks;
909 /* Avoid divide by zero */
913 tu = cputick2usec(ruxp->rux_runtime);
914 if ((int64_t)tu < 0) {
915 /* XXX: this should be an assert /phk */
916 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
917 (intmax_t)tu, p->p_pid, p->p_comm);
921 if (tu >= ruxp->rux_tu) {
923 * The normal case, time increased.
924 * Enforce monotonicity of bucketed numbers.
927 if (uu < ruxp->rux_uu)
930 if (su < ruxp->rux_su)
932 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
934 * When we calibrate the cputicker, it is not uncommon to
935 * see the presumably fixed frequency increase slightly over
936 * time as a result of thermal stabilization and NTP
937 * discipline (of the reference clock). We therefore ignore
938 * a bit of backwards slop because we expect to catch up
939 * shortly. We use a 3 microsecond limit to catch low
940 * counts and a 1% limit for high counts.
945 } else { /* tu < ruxp->rux_tu */
947 * What happened here was likely that a laptop, which ran at
948 * a reduced clock frequency at boot, kicked into high gear.
949 * The wisdom of spamming this message in that case is
950 * dubious, but it might also be indicative of something
951 * serious, so lets keep it and hope laptops can be made
952 * more truthful about their CPU speed via ACPI.
954 printf("calcru: runtime went backwards from %ju usec "
955 "to %ju usec for pid %d (%s)\n",
956 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
957 p->p_pid, p->p_comm);
966 up->tv_sec = uu / 1000000;
967 up->tv_usec = uu % 1000000;
968 sp->tv_sec = su / 1000000;
969 sp->tv_usec = su % 1000000;
972 #ifndef _SYS_SYSPROTO_H_
973 struct getrusage_args {
975 struct rusage *rusage;
979 sys_getrusage(td, uap)
980 register struct thread *td;
981 register struct getrusage_args *uap;
986 error = kern_getrusage(td, uap->who, &ru);
988 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
993 kern_getrusage(struct thread *td, int who, struct rusage *rup)
1003 rufetchcalc(p, rup, &rup->ru_utime,
1007 case RUSAGE_CHILDREN:
1008 *rup = p->p_stats->p_cru;
1009 calccru(p, &rup->ru_utime, &rup->ru_stime);
1028 rucollect(struct rusage *ru, struct rusage *ru2)
1033 if (ru->ru_maxrss < ru2->ru_maxrss)
1034 ru->ru_maxrss = ru2->ru_maxrss;
1036 ip2 = &ru2->ru_first;
1037 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1042 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1043 struct rusage_ext *rux2)
1046 rux->rux_runtime += rux2->rux_runtime;
1047 rux->rux_uticks += rux2->rux_uticks;
1048 rux->rux_sticks += rux2->rux_sticks;
1049 rux->rux_iticks += rux2->rux_iticks;
1050 rux->rux_uu += rux2->rux_uu;
1051 rux->rux_su += rux2->rux_su;
1052 rux->rux_tu += rux2->rux_tu;
1057 * Aggregate tick counts into the proc's rusage_ext.
1060 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1063 THREAD_LOCK_ASSERT(td, MA_OWNED);
1064 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1065 rux->rux_runtime += td->td_incruntime;
1066 rux->rux_uticks += td->td_uticks;
1067 rux->rux_sticks += td->td_sticks;
1068 rux->rux_iticks += td->td_iticks;
1072 ruxagg(struct proc *p, struct thread *td)
1076 ruxagg_locked(&p->p_rux, td);
1077 ruxagg_locked(&td->td_rux, td);
1078 td->td_incruntime = 0;
1086 * Update the rusage_ext structure and fetch a valid aggregate rusage
1087 * for proc p if storage for one is supplied.
1090 rufetch(struct proc *p, struct rusage *ru)
1094 PROC_SLOCK_ASSERT(p, MA_OWNED);
1097 if (p->p_numthreads > 0) {
1098 FOREACH_THREAD_IN_PROC(p, td) {
1100 rucollect(ru, &td->td_ru);
1106 * Atomically perform a rufetch and a calcru together.
1107 * Consumers, can safely assume the calcru is executed only once
1108 * rufetch is completed.
1111 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1122 * Allocate a new resource limits structure and initialize its
1123 * reference count and mutex pointer.
1128 struct plimit *limp;
1130 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1131 refcount_init(&limp->pl_refcnt, 1);
1137 struct plimit *limp;
1140 refcount_acquire(&limp->pl_refcnt);
1146 struct plimit *limp;
1149 return (limp->pl_refcnt > 1);
1153 lim_fork(struct proc *p1, struct proc *p2)
1156 PROC_LOCK_ASSERT(p1, MA_OWNED);
1157 PROC_LOCK_ASSERT(p2, MA_OWNED);
1159 p2->p_limit = lim_hold(p1->p_limit);
1160 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1161 if (p1->p_cpulimit != RLIM_INFINITY)
1162 callout_reset_sbt(&p2->p_limco, SBT_1S, 0,
1163 lim_cb, p2, C_PREL(1));
1168 struct plimit *limp;
1171 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1172 if (refcount_release(&limp->pl_refcnt))
1173 free((void *)limp, M_PLIMIT);
1177 * Make a copy of the plimit structure.
1178 * We share these structures copy-on-write after fork.
1182 struct plimit *dst, *src;
1185 KASSERT(!lim_shared(dst), ("lim_copy to shared limit"));
1186 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1190 * Return the hard limit for a particular system resource. The
1191 * which parameter specifies the index into the rlimit array.
1194 lim_max(struct proc *p, int which)
1198 lim_rlimit(p, which, &rl);
1199 return (rl.rlim_max);
1203 * Return the current (soft) limit for a particular system resource.
1204 * The which parameter which specifies the index into the rlimit array
1207 lim_cur(struct proc *p, int which)
1211 lim_rlimit(p, which, &rl);
1212 return (rl.rlim_cur);
1216 * Return a copy of the entire rlimit structure for the system limit
1217 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1220 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1223 PROC_LOCK_ASSERT(p, MA_OWNED);
1224 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1225 ("request for invalid resource limit"));
1226 *rlp = p->p_limit->pl_rlimit[which];
1227 if (p->p_sysent->sv_fixlimit != NULL)
1228 p->p_sysent->sv_fixlimit(rlp, which);
1235 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1236 rw_init(&uihashtbl_lock, "uidinfo hash");
1240 * Look up a uidinfo struct for the parameter uid.
1241 * uihashtbl_lock must be locked.
1243 static struct uidinfo *
1247 struct uihashhead *uipp;
1248 struct uidinfo *uip;
1250 rw_assert(&uihashtbl_lock, RA_LOCKED);
1252 LIST_FOREACH(uip, uipp, ui_hash)
1253 if (uip->ui_uid == uid)
1260 * Find or allocate a struct uidinfo for a particular uid.
1261 * Increase refcount on uidinfo struct returned.
1262 * uifree() should be called on a struct uidinfo when released.
1268 struct uidinfo *old_uip, *uip;
1270 rw_rlock(&uihashtbl_lock);
1271 uip = uilookup(uid);
1273 rw_runlock(&uihashtbl_lock);
1274 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1275 racct_create(&uip->ui_racct);
1276 rw_wlock(&uihashtbl_lock);
1278 * There's a chance someone created our uidinfo while we
1279 * were in malloc and not holding the lock, so we have to
1280 * make sure we don't insert a duplicate uidinfo.
1282 if ((old_uip = uilookup(uid)) != NULL) {
1283 /* Someone else beat us to it. */
1284 racct_destroy(&uip->ui_racct);
1285 free(uip, M_UIDINFO);
1288 refcount_init(&uip->ui_ref, 0);
1290 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1292 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1296 rw_unlock(&uihashtbl_lock);
1301 * Place another refcount on a uidinfo struct.
1305 struct uidinfo *uip;
1308 refcount_acquire(&uip->ui_ref);
1312 * Since uidinfo structs have a long lifetime, we use an
1313 * opportunistic refcounting scheme to avoid locking the lookup hash
1316 * If the refcount hits 0, we need to free the structure,
1317 * which means we need to lock the hash.
1319 * After locking the struct and lowering the refcount, if we find
1320 * that we don't need to free, simply unlock and return.
1322 * If refcount lowering results in need to free, bump the count
1323 * back up, lose the lock and acquire the locks in the proper
1324 * order to try again.
1328 struct uidinfo *uip;
1332 /* Prepare for optimal case. */
1334 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1337 /* Prepare for suboptimal case. */
1338 rw_wlock(&uihashtbl_lock);
1339 if (refcount_release(&uip->ui_ref)) {
1340 racct_destroy(&uip->ui_racct);
1341 LIST_REMOVE(uip, ui_hash);
1342 rw_wunlock(&uihashtbl_lock);
1343 if (uip->ui_sbsize != 0)
1344 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1345 uip->ui_uid, uip->ui_sbsize);
1346 if (uip->ui_proccnt != 0)
1347 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1348 uip->ui_uid, uip->ui_proccnt);
1349 if (uip->ui_vmsize != 0)
1350 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1351 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1352 mtx_destroy(&uip->ui_vmsize_mtx);
1353 free(uip, M_UIDINFO);
1357 * Someone added a reference between atomic_cmpset_int() and
1358 * rw_wlock(&uihashtbl_lock).
1360 rw_wunlock(&uihashtbl_lock);
1364 ui_racct_foreach(void (*callback)(struct racct *racct,
1365 void *arg2, void *arg3), void *arg2, void *arg3)
1367 struct uidinfo *uip;
1368 struct uihashhead *uih;
1370 rw_rlock(&uihashtbl_lock);
1371 for (uih = &uihashtbl[uihash]; uih >= uihashtbl; uih--) {
1372 LIST_FOREACH(uip, uih, ui_hash) {
1373 (callback)(uip->ui_racct, arg2, arg3);
1376 rw_runlock(&uihashtbl_lock);
1380 * Change the count associated with number of processes
1381 * a given user is using. When 'max' is 0, don't enforce a limit
1384 chgproccnt(uip, diff, max)
1385 struct uidinfo *uip;
1390 /* Don't allow them to exceed max, but allow subtraction. */
1391 if (diff > 0 && max != 0) {
1392 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1393 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1397 atomic_add_long(&uip->ui_proccnt, (long)diff);
1398 if (uip->ui_proccnt < 0)
1399 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1405 * Change the total socket buffer size a user has used.
1408 chgsbsize(uip, hiwat, to, max)
1409 struct uidinfo *uip;
1418 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1419 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1423 atomic_add_long(&uip->ui_sbsize, (long)diff);
1424 if (uip->ui_sbsize < 0)
1425 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1432 * Change the count associated with number of pseudo-terminals
1433 * a given user is using. When 'max' is 0, don't enforce a limit
1436 chgptscnt(uip, diff, max)
1437 struct uidinfo *uip;
1442 /* Don't allow them to exceed max, but allow subtraction. */
1443 if (diff > 0 && max != 0) {
1444 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1445 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1449 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1450 if (uip->ui_ptscnt < 0)
1451 printf("negative ptscnt for uid = %d\n", uip->ui_uid);
1457 chgkqcnt(struct uidinfo *uip, int diff, rlim_t max)
1460 if (diff > 0 && max != 0) {
1461 if (atomic_fetchadd_long(&uip->ui_kqcnt, (long)diff) +
1463 atomic_subtract_long(&uip->ui_kqcnt, (long)diff);
1467 atomic_add_long(&uip->ui_kqcnt, (long)diff);
1468 if (uip->ui_kqcnt < 0)
1469 printf("negative kqcnt for uid = %d\n", uip->ui_uid);