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);
84 * Resource controls and accounting.
86 #ifndef _SYS_SYSPROTO_H_
87 struct getpriority_args {
93 sys_getpriority(td, uap)
95 register struct getpriority_args *uap;
103 switch (uap->which) {
107 low = td->td_proc->p_nice;
112 if (p_cansee(td, p) == 0)
119 sx_slock(&proctree_lock);
121 pg = td->td_proc->p_pgrp;
124 pg = pgfind(uap->who);
126 sx_sunlock(&proctree_lock);
130 sx_sunlock(&proctree_lock);
131 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
133 if (p->p_state == PRS_NORMAL &&
134 p_cansee(td, p) == 0) {
145 uap->who = td->td_ucred->cr_uid;
146 sx_slock(&allproc_lock);
147 FOREACH_PROC_IN_SYSTEM(p) {
149 if (p->p_state == PRS_NORMAL &&
150 p_cansee(td, p) == 0 &&
151 p->p_ucred->cr_uid == uap->who) {
157 sx_sunlock(&allproc_lock);
164 if (low == PRIO_MAX + 1 && error == 0)
166 td->td_retval[0] = low;
170 #ifndef _SYS_SYSPROTO_H_
171 struct setpriority_args {
178 sys_setpriority(td, uap)
180 struct setpriority_args *uap;
182 struct proc *curp, *p;
184 int found = 0, error = 0;
187 switch (uap->which) {
191 error = donice(td, curp, uap->prio);
197 error = p_cansee(td, p);
199 error = donice(td, p, uap->prio);
206 sx_slock(&proctree_lock);
211 pg = pgfind(uap->who);
213 sx_sunlock(&proctree_lock);
217 sx_sunlock(&proctree_lock);
218 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
220 if (p->p_state == PRS_NORMAL &&
221 p_cansee(td, p) == 0) {
222 error = donice(td, p, uap->prio);
232 uap->who = td->td_ucred->cr_uid;
233 sx_slock(&allproc_lock);
234 FOREACH_PROC_IN_SYSTEM(p) {
236 if (p->p_state == PRS_NORMAL &&
237 p->p_ucred->cr_uid == uap->who &&
238 p_cansee(td, p) == 0) {
239 error = donice(td, p, uap->prio);
244 sx_sunlock(&allproc_lock);
251 if (found == 0 && error == 0)
257 * Set "nice" for a (whole) process.
260 donice(struct thread *td, struct proc *p, int n)
264 PROC_LOCK_ASSERT(p, MA_OWNED);
265 if ((error = p_cansched(td, p)))
271 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
277 static int unprivileged_idprio;
278 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_idprio, CTLFLAG_RW,
279 &unprivileged_idprio, 0, "Allow non-root users to set an idle priority");
282 * Set realtime priority for LWP.
284 #ifndef _SYS_SYSPROTO_H_
285 struct rtprio_thread_args {
292 sys_rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
299 /* Perform copyin before acquiring locks if needed. */
300 if (uap->function == RTP_SET)
301 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
305 if (uap->lwpid == 0 || uap->lwpid == td->td_tid) {
310 /* Only look up thread in current process */
311 td1 = tdfind(uap->lwpid, curproc->p_pid);
317 switch (uap->function) {
319 if ((error = p_cansee(td, p)))
321 pri_to_rtp(td1, &rtp);
323 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
325 if ((error = p_cansched(td, p)) || (error = cierror))
328 /* Disallow setting rtprio in most cases if not superuser. */
331 * Realtime priority has to be restricted for reasons which
332 * should be obvious. However, for idleprio processes, there is
333 * a potential for system deadlock if an idleprio process gains
334 * a lock on a resource that other processes need (and the
335 * idleprio process can't run due to a CPU-bound normal
336 * process). Fix me! XXX
338 * This problem is not only related to idleprio process.
339 * A user level program can obtain a file lock and hold it
340 * indefinitely. Additionally, without idleprio processes it is
341 * still conceivable that a program with low priority will never
342 * get to run. In short, allowing this feature might make it
343 * easier to lock a resource indefinitely, but it is not the
344 * only thing that makes it possible.
346 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME ||
347 (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
348 unprivileged_idprio == 0)) {
349 error = priv_check(td, PRIV_SCHED_RTPRIO);
353 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;
383 /* Perform copyin before acquiring locks if needed. */
384 if (uap->function == RTP_SET)
385 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
398 switch (uap->function) {
400 if ((error = p_cansee(td, p)))
403 * Return OUR priority if no pid specified,
404 * or if one is, report the highest priority
405 * in the process. There isn't much more you can do as
406 * there is only room to return a single priority.
407 * Note: specifying our own pid is not the same
408 * as leaving it zero.
411 pri_to_rtp(td, &rtp);
415 rtp.type = RTP_PRIO_IDLE;
416 rtp.prio = RTP_PRIO_MAX;
417 FOREACH_THREAD_IN_PROC(p, tdp) {
418 pri_to_rtp(tdp, &rtp2);
419 if (rtp2.type < rtp.type ||
420 (rtp2.type == rtp.type &&
421 rtp2.prio < rtp.prio)) {
422 rtp.type = rtp2.type;
423 rtp.prio = rtp2.prio;
428 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
430 if ((error = p_cansched(td, p)) || (error = cierror))
434 * Disallow setting rtprio in most cases if not superuser.
435 * See the comment in sys_rtprio_thread about idprio
436 * threads holding a lock.
438 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME ||
439 (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
440 !unprivileged_idprio)) {
441 error = priv_check(td, PRIV_SCHED_RTPRIO);
447 * If we are setting our own priority, set just our
448 * thread but if we are doing another process,
449 * do all the threads on that process. If we
450 * specify our own pid we do the latter.
453 error = rtp_to_pri(&rtp, td);
455 FOREACH_THREAD_IN_PROC(p, td) {
456 if ((error = rtp_to_pri(&rtp, td)) != 0)
470 rtp_to_pri(struct rtprio *rtp, struct thread *td)
472 u_char newpri, oldclass, oldpri;
474 switch (RTP_PRIO_BASE(rtp->type)) {
475 case RTP_PRIO_REALTIME:
476 if (rtp->prio > RTP_PRIO_MAX)
478 newpri = PRI_MIN_REALTIME + rtp->prio;
480 case RTP_PRIO_NORMAL:
481 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
483 newpri = PRI_MIN_TIMESHARE + rtp->prio;
486 if (rtp->prio > RTP_PRIO_MAX)
488 newpri = PRI_MIN_IDLE + rtp->prio;
495 oldclass = td->td_pri_class;
496 sched_class(td, rtp->type); /* XXX fix */
497 oldpri = td->td_user_pri;
498 sched_user_prio(td, newpri);
499 if (td->td_user_pri != oldpri && (oldclass != RTP_PRIO_NORMAL ||
500 td->td_pri_class != RTP_PRIO_NORMAL))
501 sched_prio(td, td->td_user_pri);
502 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
505 umtx_pi_adjust(td, oldpri);
513 pri_to_rtp(struct thread *td, struct rtprio *rtp)
517 switch (PRI_BASE(td->td_pri_class)) {
519 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
522 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
525 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
530 rtp->type = td->td_pri_class;
534 #if defined(COMPAT_43)
535 #ifndef _SYS_SYSPROTO_H_
536 struct osetrlimit_args {
544 register struct osetrlimit_args *uap;
550 if ((error = copyin(uap->rlp, &olim, sizeof(struct orlimit))))
552 lim.rlim_cur = olim.rlim_cur;
553 lim.rlim_max = olim.rlim_max;
554 error = kern_setrlimit(td, uap->which, &lim);
558 #ifndef _SYS_SYSPROTO_H_
559 struct ogetrlimit_args {
567 register struct ogetrlimit_args *uap;
574 if (uap->which >= RLIM_NLIMITS)
578 lim_rlimit(p, uap->which, &rl);
582 * XXX would be more correct to convert only RLIM_INFINITY to the
583 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
584 * values. Most 64->32 and 32->16 conversions, including not
585 * unimportant ones of uids are even more broken than what we
586 * do here (they blindly truncate). We don't do this correctly
587 * here since we have little experience with EOVERFLOW yet.
588 * Elsewhere, getuid() can't fail...
590 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
591 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
592 error = copyout(&olim, uap->rlp, sizeof(olim));
595 #endif /* COMPAT_43 */
597 #ifndef _SYS_SYSPROTO_H_
598 struct __setrlimit_args {
604 sys_setrlimit(td, uap)
606 register struct __setrlimit_args *uap;
611 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
613 error = kern_setrlimit(td, uap->which, &alim);
625 PROC_LOCK_ASSERT(p, MA_OWNED);
627 * Check if the process exceeds its cpu resource allocation. If
628 * it reaches the max, arrange to kill the process in ast().
630 if (p->p_cpulimit == RLIM_INFINITY)
633 FOREACH_THREAD_IN_PROC(p, td) {
637 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
638 lim_rlimit(p, RLIMIT_CPU, &rlim);
639 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
640 killproc(p, "exceeded maximum CPU limit");
642 if (p->p_cpulimit < rlim.rlim_max)
644 kern_psignal(p, SIGXCPU);
647 if ((p->p_flag & P_WEXIT) == 0)
648 callout_reset_sbt(&p->p_limco, SBT_1S, 0,
649 lim_cb, p, C_PREL(1));
653 kern_setrlimit(struct thread *td, u_int which, struct rlimit *limp)
656 return (kern_proc_setrlimit(td, td->td_proc, which, limp));
660 kern_proc_setrlimit(struct thread *td, struct proc *p, u_int which,
663 struct plimit *newlim, *oldlim;
664 register struct rlimit *alimp;
665 struct rlimit oldssiz;
668 if (which >= RLIM_NLIMITS)
672 * Preserve historical bugs by treating negative limits as unsigned.
674 if (limp->rlim_cur < 0)
675 limp->rlim_cur = RLIM_INFINITY;
676 if (limp->rlim_max < 0)
677 limp->rlim_max = RLIM_INFINITY;
679 oldssiz.rlim_cur = 0;
680 newlim = lim_alloc();
683 alimp = &oldlim->pl_rlimit[which];
684 if (limp->rlim_cur > alimp->rlim_max ||
685 limp->rlim_max > alimp->rlim_max)
686 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
691 if (limp->rlim_cur > limp->rlim_max)
692 limp->rlim_cur = limp->rlim_max;
693 lim_copy(newlim, oldlim);
694 alimp = &newlim->pl_rlimit[which];
699 if (limp->rlim_cur != RLIM_INFINITY &&
700 p->p_cpulimit == RLIM_INFINITY)
701 callout_reset_sbt(&p->p_limco, SBT_1S, 0,
702 lim_cb, p, C_PREL(1));
703 p->p_cpulimit = limp->rlim_cur;
706 if (limp->rlim_cur > maxdsiz)
707 limp->rlim_cur = maxdsiz;
708 if (limp->rlim_max > maxdsiz)
709 limp->rlim_max = maxdsiz;
713 if (limp->rlim_cur > maxssiz)
714 limp->rlim_cur = maxssiz;
715 if (limp->rlim_max > maxssiz)
716 limp->rlim_max = maxssiz;
718 if (p->p_sysent->sv_fixlimit != NULL)
719 p->p_sysent->sv_fixlimit(&oldssiz,
724 if (limp->rlim_cur > maxfilesperproc)
725 limp->rlim_cur = maxfilesperproc;
726 if (limp->rlim_max > maxfilesperproc)
727 limp->rlim_max = maxfilesperproc;
731 if (limp->rlim_cur > maxprocperuid)
732 limp->rlim_cur = maxprocperuid;
733 if (limp->rlim_max > maxprocperuid)
734 limp->rlim_max = maxprocperuid;
735 if (limp->rlim_cur < 1)
737 if (limp->rlim_max < 1)
741 if (p->p_sysent->sv_fixlimit != NULL)
742 p->p_sysent->sv_fixlimit(limp, which);
748 if (which == RLIMIT_STACK) {
750 * Stack is allocated to the max at exec time with only
751 * "rlim_cur" bytes accessible. If stack limit is going
752 * up make more accessible, if going down make inaccessible.
754 if (limp->rlim_cur != oldssiz.rlim_cur) {
759 if (limp->rlim_cur > oldssiz.rlim_cur) {
760 prot = p->p_sysent->sv_stackprot;
761 size = limp->rlim_cur - oldssiz.rlim_cur;
762 addr = p->p_sysent->sv_usrstack -
766 size = oldssiz.rlim_cur - limp->rlim_cur;
767 addr = p->p_sysent->sv_usrstack -
770 addr = trunc_page(addr);
771 size = round_page(size);
772 (void)vm_map_protect(&p->p_vmspace->vm_map,
773 addr, addr + size, prot, FALSE);
780 #ifndef _SYS_SYSPROTO_H_
781 struct __getrlimit_args {
788 sys_getrlimit(td, uap)
790 register struct __getrlimit_args *uap;
796 if (uap->which >= RLIM_NLIMITS)
800 lim_rlimit(p, uap->which, &rlim);
802 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
807 * Transform the running time and tick information for children of proc p
808 * into user and system time usage.
817 PROC_LOCK_ASSERT(p, MA_OWNED);
818 calcru1(p, &p->p_crux, up, sp);
822 * Transform the running time and tick information in proc p into user
823 * and system time usage. If appropriate, include the current time slice
827 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
832 PROC_LOCK_ASSERT(p, MA_OWNED);
833 PROC_SLOCK_ASSERT(p, MA_OWNED);
835 * If we are getting stats for the current process, then add in the
836 * stats that this thread has accumulated in its current time slice.
837 * We reset the thread and CPU state as if we had performed a context
841 if (td->td_proc == p) {
843 runtime = u - PCPU_GET(switchtime);
844 td->td_runtime += runtime;
845 td->td_incruntime += runtime;
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);
857 /* Collect resource usage for a single thread. */
859 rufetchtd(struct thread *td, struct rusage *ru)
865 PROC_SLOCK_ASSERT(p, MA_OWNED);
866 THREAD_LOCK_ASSERT(td, MA_OWNED);
868 * If we are getting stats for the current thread, then add in the
869 * stats that this thread has accumulated in its current time slice.
870 * We reset the thread and CPU state as if we had performed a context
873 if (td == curthread) {
875 runtime = u - PCPU_GET(switchtime);
876 td->td_runtime += runtime;
877 td->td_incruntime += runtime;
878 PCPU_SET(switchtime, u);
882 calcru1(p, &td->td_rux, &ru->ru_utime, &ru->ru_stime);
886 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
889 /* {user, system, interrupt, total} {ticks, usec}: */
890 uint64_t ut, uu, st, su, it, tt, tu;
892 ut = ruxp->rux_uticks;
893 st = ruxp->rux_sticks;
894 it = ruxp->rux_iticks;
897 /* Avoid divide by zero */
901 tu = cputick2usec(ruxp->rux_runtime);
902 if ((int64_t)tu < 0) {
903 /* XXX: this should be an assert /phk */
904 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
905 (intmax_t)tu, p->p_pid, p->p_comm);
909 if (tu >= ruxp->rux_tu) {
911 * The normal case, time increased.
912 * Enforce monotonicity of bucketed numbers.
915 if (uu < ruxp->rux_uu)
918 if (su < ruxp->rux_su)
920 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
922 * When we calibrate the cputicker, it is not uncommon to
923 * see the presumably fixed frequency increase slightly over
924 * time as a result of thermal stabilization and NTP
925 * discipline (of the reference clock). We therefore ignore
926 * a bit of backwards slop because we expect to catch up
927 * shortly. We use a 3 microsecond limit to catch low
928 * counts and a 1% limit for high counts.
933 } else { /* tu < ruxp->rux_tu */
935 * What happened here was likely that a laptop, which ran at
936 * a reduced clock frequency at boot, kicked into high gear.
937 * The wisdom of spamming this message in that case is
938 * dubious, but it might also be indicative of something
939 * serious, so lets keep it and hope laptops can be made
940 * more truthful about their CPU speed via ACPI.
942 printf("calcru: runtime went backwards from %ju usec "
943 "to %ju usec for pid %d (%s)\n",
944 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
945 p->p_pid, p->p_comm);
954 up->tv_sec = uu / 1000000;
955 up->tv_usec = uu % 1000000;
956 sp->tv_sec = su / 1000000;
957 sp->tv_usec = su % 1000000;
960 #ifndef _SYS_SYSPROTO_H_
961 struct getrusage_args {
963 struct rusage *rusage;
967 sys_getrusage(td, uap)
968 register struct thread *td;
969 register struct getrusage_args *uap;
974 error = kern_getrusage(td, uap->who, &ru);
976 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
981 kern_getrusage(struct thread *td, int who, struct rusage *rup)
991 rufetchcalc(p, rup, &rup->ru_utime,
995 case RUSAGE_CHILDREN:
996 *rup = p->p_stats->p_cru;
997 calccru(p, &rup->ru_utime, &rup->ru_stime);
1016 rucollect(struct rusage *ru, struct rusage *ru2)
1021 if (ru->ru_maxrss < ru2->ru_maxrss)
1022 ru->ru_maxrss = ru2->ru_maxrss;
1024 ip2 = &ru2->ru_first;
1025 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1030 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1031 struct rusage_ext *rux2)
1034 rux->rux_runtime += rux2->rux_runtime;
1035 rux->rux_uticks += rux2->rux_uticks;
1036 rux->rux_sticks += rux2->rux_sticks;
1037 rux->rux_iticks += rux2->rux_iticks;
1038 rux->rux_uu += rux2->rux_uu;
1039 rux->rux_su += rux2->rux_su;
1040 rux->rux_tu += rux2->rux_tu;
1045 * Aggregate tick counts into the proc's rusage_ext.
1048 ruxagg_locked(struct rusage_ext *rux, struct thread *td)
1051 THREAD_LOCK_ASSERT(td, MA_OWNED);
1052 PROC_SLOCK_ASSERT(td->td_proc, MA_OWNED);
1053 rux->rux_runtime += td->td_incruntime;
1054 rux->rux_uticks += td->td_uticks;
1055 rux->rux_sticks += td->td_sticks;
1056 rux->rux_iticks += td->td_iticks;
1060 ruxagg(struct proc *p, struct thread *td)
1064 ruxagg_locked(&p->p_rux, td);
1065 ruxagg_locked(&td->td_rux, td);
1066 td->td_incruntime = 0;
1074 * Update the rusage_ext structure and fetch a valid aggregate rusage
1075 * for proc p if storage for one is supplied.
1078 rufetch(struct proc *p, struct rusage *ru)
1082 PROC_SLOCK_ASSERT(p, MA_OWNED);
1085 if (p->p_numthreads > 0) {
1086 FOREACH_THREAD_IN_PROC(p, td) {
1088 rucollect(ru, &td->td_ru);
1094 * Atomically perform a rufetch and a calcru together.
1095 * Consumers, can safely assume the calcru is executed only once
1096 * rufetch is completed.
1099 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1110 * Allocate a new resource limits structure and initialize its
1111 * reference count and mutex pointer.
1116 struct plimit *limp;
1118 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1119 refcount_init(&limp->pl_refcnt, 1);
1125 struct plimit *limp;
1128 refcount_acquire(&limp->pl_refcnt);
1133 lim_fork(struct proc *p1, struct proc *p2)
1136 PROC_LOCK_ASSERT(p1, MA_OWNED);
1137 PROC_LOCK_ASSERT(p2, MA_OWNED);
1139 p2->p_limit = lim_hold(p1->p_limit);
1140 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1141 if (p1->p_cpulimit != RLIM_INFINITY)
1142 callout_reset_sbt(&p2->p_limco, SBT_1S, 0,
1143 lim_cb, p2, C_PREL(1));
1148 struct plimit *limp;
1151 KASSERT(limp->pl_refcnt > 0, ("plimit refcnt underflow"));
1152 if (refcount_release(&limp->pl_refcnt))
1153 free((void *)limp, M_PLIMIT);
1157 * Make a copy of the plimit structure.
1158 * We share these structures copy-on-write after fork.
1162 struct plimit *dst, *src;
1165 KASSERT(dst->pl_refcnt == 1, ("lim_copy to shared limit"));
1166 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1170 * Return the hard limit for a particular system resource. The
1171 * which parameter specifies the index into the rlimit array.
1174 lim_max(struct proc *p, int which)
1178 lim_rlimit(p, which, &rl);
1179 return (rl.rlim_max);
1183 * Return the current (soft) limit for a particular system resource.
1184 * The which parameter which specifies the index into the rlimit array
1187 lim_cur(struct proc *p, int which)
1191 lim_rlimit(p, which, &rl);
1192 return (rl.rlim_cur);
1196 * Return a copy of the entire rlimit structure for the system limit
1197 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1200 lim_rlimit(struct proc *p, int which, struct rlimit *rlp)
1203 PROC_LOCK_ASSERT(p, MA_OWNED);
1204 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1205 ("request for invalid resource limit"));
1206 *rlp = p->p_limit->pl_rlimit[which];
1207 if (p->p_sysent->sv_fixlimit != NULL)
1208 p->p_sysent->sv_fixlimit(rlp, which);
1215 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1216 rw_init(&uihashtbl_lock, "uidinfo hash");
1220 * Look up a uidinfo struct for the parameter uid.
1221 * uihashtbl_lock must be locked.
1223 static struct uidinfo *
1227 struct uihashhead *uipp;
1228 struct uidinfo *uip;
1230 rw_assert(&uihashtbl_lock, RA_LOCKED);
1232 LIST_FOREACH(uip, uipp, ui_hash)
1233 if (uip->ui_uid == uid)
1240 * Find or allocate a struct uidinfo for a particular uid.
1241 * Increase refcount on uidinfo struct returned.
1242 * uifree() should be called on a struct uidinfo when released.
1248 struct uidinfo *old_uip, *uip;
1250 rw_rlock(&uihashtbl_lock);
1251 uip = uilookup(uid);
1253 rw_runlock(&uihashtbl_lock);
1254 uip = malloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
1255 racct_create(&uip->ui_racct);
1256 rw_wlock(&uihashtbl_lock);
1258 * There's a chance someone created our uidinfo while we
1259 * were in malloc and not holding the lock, so we have to
1260 * make sure we don't insert a duplicate uidinfo.
1262 if ((old_uip = uilookup(uid)) != NULL) {
1263 /* Someone else beat us to it. */
1264 racct_destroy(&uip->ui_racct);
1265 free(uip, M_UIDINFO);
1268 refcount_init(&uip->ui_ref, 0);
1270 mtx_init(&uip->ui_vmsize_mtx, "ui_vmsize", NULL,
1272 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
1276 rw_unlock(&uihashtbl_lock);
1281 * Place another refcount on a uidinfo struct.
1285 struct uidinfo *uip;
1288 refcount_acquire(&uip->ui_ref);
1292 * Since uidinfo structs have a long lifetime, we use an
1293 * opportunistic refcounting scheme to avoid locking the lookup hash
1296 * If the refcount hits 0, we need to free the structure,
1297 * which means we need to lock the hash.
1299 * After locking the struct and lowering the refcount, if we find
1300 * that we don't need to free, simply unlock and return.
1302 * If refcount lowering results in need to free, bump the count
1303 * back up, lose the lock and acquire the locks in the proper
1304 * order to try again.
1308 struct uidinfo *uip;
1312 /* Prepare for optimal case. */
1314 if (old > 1 && atomic_cmpset_int(&uip->ui_ref, old, old - 1))
1317 /* Prepare for suboptimal case. */
1318 rw_wlock(&uihashtbl_lock);
1319 if (refcount_release(&uip->ui_ref)) {
1320 racct_destroy(&uip->ui_racct);
1321 LIST_REMOVE(uip, ui_hash);
1322 rw_wunlock(&uihashtbl_lock);
1323 if (uip->ui_sbsize != 0)
1324 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1325 uip->ui_uid, uip->ui_sbsize);
1326 if (uip->ui_proccnt != 0)
1327 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1328 uip->ui_uid, uip->ui_proccnt);
1329 if (uip->ui_vmsize != 0)
1330 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1331 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1332 mtx_destroy(&uip->ui_vmsize_mtx);
1333 free(uip, M_UIDINFO);
1337 * Someone added a reference between atomic_cmpset_int() and
1338 * rw_wlock(&uihashtbl_lock).
1340 rw_wunlock(&uihashtbl_lock);
1344 ui_racct_foreach(void (*callback)(struct racct *racct,
1345 void *arg2, void *arg3), void *arg2, void *arg3)
1347 struct uidinfo *uip;
1348 struct uihashhead *uih;
1350 rw_rlock(&uihashtbl_lock);
1351 for (uih = &uihashtbl[uihash]; uih >= uihashtbl; uih--) {
1352 LIST_FOREACH(uip, uih, ui_hash) {
1353 (callback)(uip->ui_racct, arg2, arg3);
1356 rw_runlock(&uihashtbl_lock);
1360 * Change the count associated with number of processes
1361 * a given user is using. When 'max' is 0, don't enforce a limit
1364 chgproccnt(uip, diff, max)
1365 struct uidinfo *uip;
1370 /* Don't allow them to exceed max, but allow subtraction. */
1371 if (diff > 0 && max != 0) {
1372 if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
1373 atomic_subtract_long(&uip->ui_proccnt, (long)diff);
1377 atomic_add_long(&uip->ui_proccnt, (long)diff);
1378 if (uip->ui_proccnt < 0)
1379 printf("negative proccnt for uid = %d\n", uip->ui_uid);
1385 * Change the total socket buffer size a user has used.
1388 chgsbsize(uip, hiwat, to, max)
1389 struct uidinfo *uip;
1398 if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
1399 atomic_subtract_long(&uip->ui_sbsize, (long)diff);
1403 atomic_add_long(&uip->ui_sbsize, (long)diff);
1404 if (uip->ui_sbsize < 0)
1405 printf("negative sbsize for uid = %d\n", uip->ui_uid);
1412 * Change the count associated with number of pseudo-terminals
1413 * a given user is using. When 'max' is 0, don't enforce a limit
1416 chgptscnt(uip, diff, max)
1417 struct uidinfo *uip;
1422 /* Don't allow them to exceed max, but allow subtraction. */
1423 if (diff > 0 && max != 0) {
1424 if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
1425 atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
1429 atomic_add_long(&uip->ui_ptscnt, (long)diff);
1430 if (uip->ui_ptscnt < 0)
1431 printf("negative ptscnt for uid = %d\n", uip->ui_uid);