2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/refcount.h>
51 #include <sys/racct.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
54 #include <sys/sched.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
60 #include <sys/umtxvar.h>
63 #include <vm/vm_param.h>
65 #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 rwlock uihashtbl_lock;
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);
78 static void ruxagg_ext_locked(struct rusage_ext *rux, struct thread *td);
81 * Resource controls and accounting.
83 #ifndef _SYS_SYSPROTO_H_
84 struct getpriority_args {
90 sys_getpriority(struct thread *td, struct getpriority_args *uap)
93 return (kern_getpriority(td, uap->which, uap->who));
97 kern_getpriority(struct thread *td, int which, int who)
108 low = td->td_proc->p_nice;
113 if (p_cansee(td, p) == 0)
120 sx_slock(&proctree_lock);
122 pg = td->td_proc->p_pgrp;
127 sx_sunlock(&proctree_lock);
131 sx_sunlock(&proctree_lock);
132 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
134 if (p->p_state == PRS_NORMAL &&
135 p_cansee(td, p) == 0) {
146 who = td->td_ucred->cr_uid;
147 sx_slock(&allproc_lock);
148 FOREACH_PROC_IN_SYSTEM(p) {
150 if (p->p_state == PRS_NORMAL &&
151 p_cansee(td, p) == 0 &&
152 p->p_ucred->cr_uid == who) {
158 sx_sunlock(&allproc_lock);
165 if (low == PRIO_MAX + 1 && error == 0)
167 td->td_retval[0] = low;
171 #ifndef _SYS_SYSPROTO_H_
172 struct setpriority_args {
179 sys_setpriority(struct thread *td, struct setpriority_args *uap)
182 return (kern_setpriority(td, uap->which, uap->who, uap->prio));
186 kern_setpriority(struct thread *td, int which, int who, int prio)
188 struct proc *curp, *p;
190 int found = 0, error = 0;
197 error = donice(td, curp, prio);
203 error = p_cansee(td, p);
205 error = donice(td, p, prio);
212 sx_slock(&proctree_lock);
219 sx_sunlock(&proctree_lock);
223 sx_sunlock(&proctree_lock);
224 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
226 if (p->p_state == PRS_NORMAL &&
227 p_cansee(td, p) == 0) {
228 error = donice(td, p, prio);
238 who = td->td_ucred->cr_uid;
239 sx_slock(&allproc_lock);
240 FOREACH_PROC_IN_SYSTEM(p) {
242 if (p->p_state == PRS_NORMAL &&
243 p->p_ucred->cr_uid == who &&
244 p_cansee(td, p) == 0) {
245 error = donice(td, p, prio);
250 sx_sunlock(&allproc_lock);
257 if (found == 0 && error == 0)
263 * Set "nice" for a (whole) process.
266 donice(struct thread *td, struct proc *p, int n)
270 PROC_LOCK_ASSERT(p, MA_OWNED);
271 if ((error = p_cansched(td, p)))
277 if (n < p->p_nice && priv_check(td, PRIV_SCHED_SETPRIORITY) != 0)
283 static int unprivileged_idprio;
284 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_idprio, CTLFLAG_RW,
285 &unprivileged_idprio, 0,
286 "Allow non-root users to set an idle priority (deprecated)");
289 * Set realtime priority for LWP.
291 #ifndef _SYS_SYSPROTO_H_
292 struct rtprio_thread_args {
299 sys_rtprio_thread(struct thread *td, struct rtprio_thread_args *uap)
306 /* Perform copyin before acquiring locks if needed. */
307 if (uap->function == RTP_SET)
308 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
312 if (uap->lwpid == 0 || uap->lwpid == td->td_tid) {
317 td1 = tdfind(uap->lwpid, -1);
323 switch (uap->function) {
325 if ((error = p_cansee(td, p)))
327 pri_to_rtp(td1, &rtp);
329 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
331 if ((error = p_cansched(td, p)) || (error = cierror))
334 /* Disallow setting rtprio in most cases if not superuser. */
337 * Realtime priority has to be restricted for reasons which
338 * should be obvious. However, for idleprio processes, there is
339 * a potential for system deadlock if an idleprio process gains
340 * a lock on a resource that other processes need (and the
341 * idleprio process can't run due to a CPU-bound normal
342 * process). Fix me! XXX
344 * This problem is not only related to idleprio process.
345 * A user level program can obtain a file lock and hold it
346 * indefinitely. Additionally, without idleprio processes it is
347 * still conceivable that a program with low priority will never
348 * get to run. In short, allowing this feature might make it
349 * easier to lock a resource indefinitely, but it is not the
350 * only thing that makes it possible.
352 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME &&
353 (error = priv_check(td, PRIV_SCHED_RTPRIO)) != 0)
355 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
356 unprivileged_idprio == 0 &&
357 (error = priv_check(td, PRIV_SCHED_IDPRIO)) != 0)
359 error = rtp_to_pri(&rtp, td1);
370 * Set realtime priority.
372 #ifndef _SYS_SYSPROTO_H_
380 sys_rtprio(struct thread *td, struct rtprio_args *uap)
387 /* Perform copyin before acquiring locks if needed. */
388 if (uap->function == RTP_SET)
389 cierror = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
402 switch (uap->function) {
404 if ((error = p_cansee(td, p)))
407 * Return OUR priority if no pid specified,
408 * or if one is, report the highest priority
409 * in the process. There isn't much more you can do as
410 * there is only room to return a single priority.
411 * Note: specifying our own pid is not the same
412 * as leaving it zero.
415 pri_to_rtp(td, &rtp);
419 rtp.type = RTP_PRIO_IDLE;
420 rtp.prio = RTP_PRIO_MAX;
421 FOREACH_THREAD_IN_PROC(p, tdp) {
422 pri_to_rtp(tdp, &rtp2);
423 if (rtp2.type < rtp.type ||
424 (rtp2.type == rtp.type &&
425 rtp2.prio < rtp.prio)) {
426 rtp.type = rtp2.type;
427 rtp.prio = rtp2.prio;
432 return (copyout(&rtp, uap->rtp, sizeof(struct rtprio)));
434 if ((error = p_cansched(td, p)) || (error = cierror))
438 * Disallow setting rtprio in most cases if not superuser.
439 * See the comment in sys_rtprio_thread about idprio
440 * threads holding a lock.
442 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME &&
443 (error = priv_check(td, PRIV_SCHED_RTPRIO)) != 0)
445 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_IDLE &&
446 unprivileged_idprio == 0 &&
447 (error = priv_check(td, PRIV_SCHED_IDPRIO)) != 0)
451 * If we are setting our own priority, set just our
452 * thread but if we are doing another process,
453 * do all the threads on that process. If we
454 * specify our own pid we do the latter.
457 error = rtp_to_pri(&rtp, td);
459 FOREACH_THREAD_IN_PROC(p, td) {
460 if ((error = rtp_to_pri(&rtp, td)) != 0)
474 rtp_to_pri(struct rtprio *rtp, struct thread *td)
476 u_char newpri, oldclass, oldpri;
478 switch (RTP_PRIO_BASE(rtp->type)) {
479 case RTP_PRIO_REALTIME:
480 if (rtp->prio > RTP_PRIO_MAX)
482 newpri = PRI_MIN_REALTIME + rtp->prio;
484 case RTP_PRIO_NORMAL:
485 if (rtp->prio > (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE))
487 newpri = PRI_MIN_TIMESHARE + rtp->prio;
490 if (rtp->prio > RTP_PRIO_MAX)
492 newpri = PRI_MIN_IDLE + rtp->prio;
499 oldclass = td->td_pri_class;
500 sched_class(td, rtp->type); /* XXX fix */
501 oldpri = td->td_user_pri;
502 sched_user_prio(td, newpri);
503 if (td->td_user_pri != oldpri && (oldclass != RTP_PRIO_NORMAL ||
504 td->td_pri_class != RTP_PRIO_NORMAL))
505 sched_prio(td, td->td_user_pri);
506 if (TD_ON_UPILOCK(td) && oldpri != newpri) {
509 umtx_pi_adjust(td, oldpri);
517 pri_to_rtp(struct thread *td, struct rtprio *rtp)
521 switch (PRI_BASE(td->td_pri_class)) {
523 rtp->prio = td->td_base_user_pri - PRI_MIN_REALTIME;
526 rtp->prio = td->td_base_user_pri - PRI_MIN_TIMESHARE;
529 rtp->prio = td->td_base_user_pri - PRI_MIN_IDLE;
534 rtp->type = td->td_pri_class;
538 #if defined(COMPAT_43)
539 #ifndef _SYS_SYSPROTO_H_
540 struct osetrlimit_args {
546 osetrlimit(struct thread *td, 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 {
567 ogetrlimit(struct thread *td, struct ogetrlimit_args *uap)
573 if (uap->which >= RLIM_NLIMITS)
575 lim_rlimit(td, uap->which, &rl);
578 * XXX would be more correct to convert only RLIM_INFINITY to the
579 * old RLIM_INFINITY and fail with EOVERFLOW for other larger
580 * values. Most 64->32 and 32->16 conversions, including not
581 * unimportant ones of uids are even more broken than what we
582 * do here (they blindly truncate). We don't do this correctly
583 * here since we have little experience with EOVERFLOW yet.
584 * Elsewhere, getuid() can't fail...
586 olim.rlim_cur = rl.rlim_cur > 0x7fffffff ? 0x7fffffff : rl.rlim_cur;
587 olim.rlim_max = rl.rlim_max > 0x7fffffff ? 0x7fffffff : rl.rlim_max;
588 error = copyout(&olim, uap->rlp, sizeof(olim));
591 #endif /* COMPAT_43 */
593 #ifndef _SYS_SYSPROTO_H_
594 struct __setrlimit_args {
600 sys_setrlimit(struct thread *td, struct __setrlimit_args *uap)
605 if ((error = copyin(uap->rlp, &alim, sizeof(struct rlimit))))
607 error = kern_setrlimit(td, uap->which, &alim);
619 PROC_LOCK_ASSERT(p, MA_OWNED);
621 * Check if the process exceeds its cpu resource allocation. If
622 * it reaches the max, arrange to kill the process in ast().
624 if (p->p_cpulimit == RLIM_INFINITY)
627 FOREACH_THREAD_IN_PROC(p, td) {
631 if (p->p_rux.rux_runtime > p->p_cpulimit * cpu_tickrate()) {
632 lim_rlimit_proc(p, RLIMIT_CPU, &rlim);
633 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
634 killproc(p, "exceeded maximum CPU limit");
636 if (p->p_cpulimit < rlim.rlim_max)
638 kern_psignal(p, SIGXCPU);
641 if ((p->p_flag & P_WEXIT) == 0)
642 callout_reset_sbt(&p->p_limco, SBT_1S, 0,
643 lim_cb, p, C_PREL(1));
647 kern_setrlimit(struct thread *td, u_int which, struct rlimit *limp)
650 return (kern_proc_setrlimit(td, td->td_proc, which, limp));
654 kern_proc_setrlimit(struct thread *td, struct proc *p, u_int which,
657 struct plimit *newlim, *oldlim;
658 struct rlimit *alimp;
659 struct rlimit oldssiz;
662 if (which >= RLIM_NLIMITS)
666 * Preserve historical bugs by treating negative limits as unsigned.
668 if (limp->rlim_cur < 0)
669 limp->rlim_cur = RLIM_INFINITY;
670 if (limp->rlim_max < 0)
671 limp->rlim_max = RLIM_INFINITY;
673 oldssiz.rlim_cur = 0;
674 newlim = lim_alloc();
677 alimp = &oldlim->pl_rlimit[which];
678 if (limp->rlim_cur > alimp->rlim_max ||
679 limp->rlim_max > alimp->rlim_max)
680 if ((error = priv_check(td, PRIV_PROC_SETRLIMIT))) {
685 if (limp->rlim_cur > limp->rlim_max)
686 limp->rlim_cur = limp->rlim_max;
687 lim_copy(newlim, oldlim);
688 alimp = &newlim->pl_rlimit[which];
692 if (limp->rlim_cur != RLIM_INFINITY &&
693 p->p_cpulimit == RLIM_INFINITY)
694 callout_reset_sbt(&p->p_limco, SBT_1S, 0,
695 lim_cb, p, C_PREL(1));
696 p->p_cpulimit = limp->rlim_cur;
699 if (limp->rlim_cur > maxdsiz)
700 limp->rlim_cur = maxdsiz;
701 if (limp->rlim_max > maxdsiz)
702 limp->rlim_max = maxdsiz;
706 if (limp->rlim_cur > maxssiz)
707 limp->rlim_cur = maxssiz;
708 if (limp->rlim_max > maxssiz)
709 limp->rlim_max = maxssiz;
711 if (p->p_sysent->sv_fixlimit != NULL)
712 p->p_sysent->sv_fixlimit(&oldssiz,
717 if (limp->rlim_cur > maxfilesperproc)
718 limp->rlim_cur = maxfilesperproc;
719 if (limp->rlim_max > maxfilesperproc)
720 limp->rlim_max = maxfilesperproc;
724 if (limp->rlim_cur > maxprocperuid)
725 limp->rlim_cur = maxprocperuid;
726 if (limp->rlim_max > maxprocperuid)
727 limp->rlim_max = maxprocperuid;
728 if (limp->rlim_cur < 1)
730 if (limp->rlim_max < 1)
734 if (p->p_sysent->sv_fixlimit != NULL)
735 p->p_sysent->sv_fixlimit(limp, which);
742 if (which == RLIMIT_STACK &&
744 * Skip calls from exec_new_vmspace(), done when stack is
747 (td != curthread || (p->p_flag & P_INEXEC) == 0)) {
749 * Stack is allocated to the max at exec time with only
750 * "rlim_cur" bytes accessible. If stack limit is going
751 * up make more accessible, if going down make inaccessible.
753 if (limp->rlim_cur != oldssiz.rlim_cur) {
758 if (limp->rlim_cur > oldssiz.rlim_cur) {
759 prot = p->p_sysent->sv_stackprot;
760 size = limp->rlim_cur - oldssiz.rlim_cur;
761 addr = round_page(p->p_vmspace->vm_stacktop) -
765 size = oldssiz.rlim_cur - limp->rlim_cur;
766 addr = round_page(p->p_vmspace->vm_stacktop) -
769 addr = trunc_page(addr);
770 size = round_page(size);
771 (void)vm_map_protect(&p->p_vmspace->vm_map,
772 addr, addr + size, prot, 0,
773 VM_MAP_PROTECT_SET_PROT);
780 #ifndef _SYS_SYSPROTO_H_
781 struct __getrlimit_args {
788 sys_getrlimit(struct thread *td, struct __getrlimit_args *uap)
793 if (uap->which >= RLIM_NLIMITS)
795 lim_rlimit(td, uap->which, &rlim);
796 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit));
801 * Transform the running time and tick information for children of proc p
802 * into user and system time usage.
805 calccru(struct proc *p, struct timeval *up, struct timeval *sp)
808 PROC_LOCK_ASSERT(p, MA_OWNED);
809 calcru1(p, &p->p_crux, up, sp);
813 * Transform the running time and tick information in proc p into user
814 * and system time usage. If appropriate, include the current time slice
818 calcru(struct proc *p, struct timeval *up, struct timeval *sp)
823 PROC_LOCK_ASSERT(p, MA_OWNED);
824 PROC_STATLOCK_ASSERT(p, MA_OWNED);
826 * If we are getting stats for the current process, then add in the
827 * stats that this thread has accumulated in its current time slice.
828 * We reset the thread and CPU state as if we had performed a context
832 if (td->td_proc == p) {
834 runtime = u - PCPU_GET(switchtime);
835 td->td_runtime += runtime;
836 td->td_incruntime += runtime;
837 PCPU_SET(switchtime, u);
839 /* Make sure the per-thread stats are current. */
840 FOREACH_THREAD_IN_PROC(p, td) {
841 if (td->td_incruntime == 0)
845 calcru1(p, &p->p_rux, up, sp);
848 /* Collect resource usage for a single thread. */
850 rufetchtd(struct thread *td, struct rusage *ru)
856 PROC_STATLOCK_ASSERT(p, MA_OWNED);
857 THREAD_LOCK_ASSERT(td, MA_OWNED);
859 * If we are getting stats for the current thread, then add in the
860 * stats that this thread has accumulated in its current time slice.
861 * We reset the thread and CPU state as if we had performed a context
864 if (td == curthread) {
866 runtime = u - PCPU_GET(switchtime);
867 td->td_runtime += runtime;
868 td->td_incruntime += runtime;
869 PCPU_SET(switchtime, u);
871 ruxagg_locked(p, td);
873 calcru1(p, &td->td_rux, &ru->ru_utime, &ru->ru_stime);
876 /* XXX: the MI version is too slow to use: */
877 #ifndef __HAVE_INLINE_FLSLL
878 #define flsll(x) (fls((x) >> 32) != 0 ? fls((x) >> 32) + 32 : fls(x))
882 mul64_by_fraction(uint64_t a, uint64_t b, uint64_t c)
884 uint64_t acc, bh, bl;
888 * Calculate (a * b) / c accurately enough without overflowing. c
889 * must be nonzero, and its top bit must be 0. a or b must be
890 * <= c, and the implementation is tuned for b <= c.
892 * The comments about times are for use in calcru1() with units of
893 * microseconds for 'a' and stathz ticks at 128 Hz for b and c.
895 * Let n be the number of top zero bits in c. Each iteration
896 * either returns, or reduces b by right shifting it by at least n.
897 * The number of iterations is at most 1 + 64 / n, and the error is
898 * at most the number of iterations.
900 * It is very unusual to need even 2 iterations. Previous
901 * implementations overflowed essentially by returning early in the
902 * first iteration, with n = 38 giving overflow at 105+ hours and
903 * n = 32 giving overlow at at 388+ days despite a more careful
904 * calculation. 388 days is a reasonable uptime, and the calculation
905 * needs to work for the uptime times the number of CPUs since 'a'
908 if (a >= (uint64_t)1 << 63)
909 return (0); /* Unsupported arg -- can't happen. */
911 for (i = 0; i < 128; i++) {
915 /* Up to 105 hours on first iteration. */
916 return (acc + (a * b) / c);
919 * This reduction is based on a = q * c + r, with the
920 * remainder r < c. 'a' may be large to start, and
921 * moving bits from b into 'a' at the end of the loop
922 * sets the top bit of 'a', so the reduction makes
923 * significant progress.
929 /* Up to 388 days on first iteration. */
930 return (acc + (a * b) / c);
934 * This step writes a * b as a * ((bh << s) + bl) =
935 * a * (bh << s) + a * bl = (a << s) * bh + a * bl. The 2
936 * additive terms are handled separately. Splitting in
937 * this way is linear except for rounding errors.
939 * s = 64 - sa is the maximum such that a << s fits in 64
940 * bits. Since a < c and c has at least 1 zero top bit,
941 * sa < 64 and s > 0. Thus this step makes progress by
942 * reducing b (it increases 'a', but taking remainders on
943 * the next iteration completes the reduction).
945 * Finally, the choice for s is just what is needed to keep
946 * a * bl from overflowing, so we don't need complications
947 * like a recursive call mul64_by_fraction(a, bl, c) to
948 * handle the second additive term.
957 return (0); /* Algorithm failure -- can't happen. */
961 calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up,
964 /* {user, system, interrupt, total} {ticks, usec}: */
965 uint64_t ut, uu, st, su, it, tt, tu;
967 ut = ruxp->rux_uticks;
968 st = ruxp->rux_sticks;
969 it = ruxp->rux_iticks;
972 /* Avoid divide by zero */
976 tu = cputick2usec(ruxp->rux_runtime);
977 if ((int64_t)tu < 0) {
978 /* XXX: this should be an assert /phk */
979 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n",
980 (intmax_t)tu, p->p_pid, p->p_comm);
984 /* Subdivide tu. Avoid overflow in the multiplications. */
985 if (__predict_true(tu <= ((uint64_t)1 << 38) && tt <= (1 << 26))) {
986 /* Up to 76 hours when stathz is 128. */
990 uu = mul64_by_fraction(tu, ut, tt);
991 su = mul64_by_fraction(tu, st, tt);
994 if (tu >= ruxp->rux_tu) {
996 * The normal case, time increased.
997 * Enforce monotonicity of bucketed numbers.
999 if (uu < ruxp->rux_uu)
1001 if (su < ruxp->rux_su)
1003 } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) {
1005 * When we calibrate the cputicker, it is not uncommon to
1006 * see the presumably fixed frequency increase slightly over
1007 * time as a result of thermal stabilization and NTP
1008 * discipline (of the reference clock). We therefore ignore
1009 * a bit of backwards slop because we expect to catch up
1010 * shortly. We use a 3 microsecond limit to catch low
1011 * counts and a 1% limit for high counts.
1016 } else { /* tu < ruxp->rux_tu */
1018 * What happened here was likely that a laptop, which ran at
1019 * a reduced clock frequency at boot, kicked into high gear.
1020 * The wisdom of spamming this message in that case is
1021 * dubious, but it might also be indicative of something
1022 * serious, so lets keep it and hope laptops can be made
1023 * more truthful about their CPU speed via ACPI.
1025 printf("calcru: runtime went backwards from %ju usec "
1026 "to %ju usec for pid %d (%s)\n",
1027 (uintmax_t)ruxp->rux_tu, (uintmax_t)tu,
1028 p->p_pid, p->p_comm);
1035 up->tv_sec = uu / 1000000;
1036 up->tv_usec = uu % 1000000;
1037 sp->tv_sec = su / 1000000;
1038 sp->tv_usec = su % 1000000;
1041 #ifndef _SYS_SYSPROTO_H_
1042 struct getrusage_args {
1044 struct rusage *rusage;
1048 sys_getrusage(struct thread *td, struct getrusage_args *uap)
1053 error = kern_getrusage(td, uap->who, &ru);
1055 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
1060 kern_getrusage(struct thread *td, int who, struct rusage *rup)
1070 rufetchcalc(p, rup, &rup->ru_utime,
1074 case RUSAGE_CHILDREN:
1075 *rup = p->p_stats->p_cru;
1076 calccru(p, &rup->ru_utime, &rup->ru_stime);
1095 rucollect(struct rusage *ru, struct rusage *ru2)
1100 if (ru->ru_maxrss < ru2->ru_maxrss)
1101 ru->ru_maxrss = ru2->ru_maxrss;
1103 ip2 = &ru2->ru_first;
1104 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1109 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
1110 struct rusage_ext *rux2)
1113 rux->rux_runtime += rux2->rux_runtime;
1114 rux->rux_uticks += rux2->rux_uticks;
1115 rux->rux_sticks += rux2->rux_sticks;
1116 rux->rux_iticks += rux2->rux_iticks;
1117 rux->rux_uu += rux2->rux_uu;
1118 rux->rux_su += rux2->rux_su;
1119 rux->rux_tu += rux2->rux_tu;
1124 * Aggregate tick counts into the proc's rusage_ext.
1127 ruxagg_ext_locked(struct rusage_ext *rux, struct thread *td)
1130 rux->rux_runtime += td->td_incruntime;
1131 rux->rux_uticks += td->td_uticks;
1132 rux->rux_sticks += td->td_sticks;
1133 rux->rux_iticks += td->td_iticks;
1137 ruxagg_locked(struct proc *p, struct thread *td)
1139 THREAD_LOCK_ASSERT(td, MA_OWNED);
1140 PROC_STATLOCK_ASSERT(td->td_proc, MA_OWNED);
1142 ruxagg_ext_locked(&p->p_rux, td);
1143 ruxagg_ext_locked(&td->td_rux, td);
1144 td->td_incruntime = 0;
1151 ruxagg(struct proc *p, struct thread *td)
1155 ruxagg_locked(p, td);
1160 * Update the rusage_ext structure and fetch a valid aggregate rusage
1161 * for proc p if storage for one is supplied.
1164 rufetch(struct proc *p, struct rusage *ru)
1168 PROC_STATLOCK_ASSERT(p, MA_OWNED);
1171 if (p->p_numthreads > 0) {
1172 FOREACH_THREAD_IN_PROC(p, td) {
1174 rucollect(ru, &td->td_ru);
1180 * Atomically perform a rufetch and a calcru together.
1181 * Consumers, can safely assume the calcru is executed only once
1182 * rufetch is completed.
1185 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
1196 * Allocate a new resource limits structure and initialize its
1197 * reference count and mutex pointer.
1202 struct plimit *limp;
1204 limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1205 refcount_init(&limp->pl_refcnt, 1);
1210 lim_hold(struct plimit *limp)
1213 refcount_acquire(&limp->pl_refcnt);
1218 lim_fork(struct proc *p1, struct proc *p2)
1221 PROC_LOCK_ASSERT(p1, MA_OWNED);
1222 PROC_LOCK_ASSERT(p2, MA_OWNED);
1224 p2->p_limit = lim_hold(p1->p_limit);
1225 callout_init_mtx(&p2->p_limco, &p2->p_mtx, 0);
1226 if (p1->p_cpulimit != RLIM_INFINITY)
1227 callout_reset_sbt(&p2->p_limco, SBT_1S, 0,
1228 lim_cb, p2, C_PREL(1));
1232 lim_free(struct plimit *limp)
1235 if (refcount_release(&limp->pl_refcnt))
1236 free((void *)limp, M_PLIMIT);
1240 lim_freen(struct plimit *limp, int n)
1243 if (refcount_releasen(&limp->pl_refcnt, n))
1244 free((void *)limp, M_PLIMIT);
1248 * Make a copy of the plimit structure.
1249 * We share these structures copy-on-write after fork.
1252 lim_copy(struct plimit *dst, struct plimit *src)
1255 KASSERT(dst->pl_refcnt <= 1, ("lim_copy to shared limit"));
1256 bcopy(src->pl_rlimit, dst->pl_rlimit, sizeof(src->pl_rlimit));
1260 * Return the hard limit for a particular system resource. The
1261 * which parameter specifies the index into the rlimit array.
1264 lim_max(struct thread *td, int which)
1268 lim_rlimit(td, which, &rl);
1269 return (rl.rlim_max);
1273 lim_max_proc(struct proc *p, int which)
1277 lim_rlimit_proc(p, which, &rl);
1278 return (rl.rlim_max);
1282 * Return the current (soft) limit for a particular system resource.
1283 * The which parameter which specifies the index into the rlimit array
1286 (lim_cur)(struct thread *td, int which)
1290 lim_rlimit(td, which, &rl);
1291 return (rl.rlim_cur);
1295 lim_cur_proc(struct proc *p, int which)
1299 lim_rlimit_proc(p, which, &rl);
1300 return (rl.rlim_cur);
1304 * Return a copy of the entire rlimit structure for the system limit
1305 * specified by 'which' in the rlimit structure pointed to by 'rlp'.
1308 lim_rlimit(struct thread *td, int which, struct rlimit *rlp)
1310 struct proc *p = td->td_proc;
1312 MPASS(td == curthread);
1313 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1314 ("request for invalid resource limit"));
1315 *rlp = td->td_limit->pl_rlimit[which];
1316 if (p->p_sysent->sv_fixlimit != NULL)
1317 p->p_sysent->sv_fixlimit(rlp, which);
1321 lim_rlimit_proc(struct proc *p, int which, struct rlimit *rlp)
1324 PROC_LOCK_ASSERT(p, MA_OWNED);
1325 KASSERT(which >= 0 && which < RLIM_NLIMITS,
1326 ("request for invalid resource limit"));
1327 *rlp = p->p_limit->pl_rlimit[which];
1328 if (p->p_sysent->sv_fixlimit != NULL)
1329 p->p_sysent->sv_fixlimit(rlp, which);
1336 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
1337 rw_init(&uihashtbl_lock, "uidinfo hash");
1341 * Look up a uidinfo struct for the parameter uid.
1342 * uihashtbl_lock must be locked.
1343 * Increase refcount on uidinfo struct returned.
1345 static struct uidinfo *
1348 struct uihashhead *uipp;
1349 struct uidinfo *uip;
1351 rw_assert(&uihashtbl_lock, RA_LOCKED);
1353 LIST_FOREACH(uip, uipp, ui_hash)
1354 if (uip->ui_uid == uid) {
1363 * Find or allocate a struct uidinfo for a particular uid.
1364 * Returns with uidinfo struct referenced.
1365 * uifree() should be called on a struct uidinfo when released.
1370 struct uidinfo *new_uip, *uip;
1373 cred = curthread->td_ucred;
1374 if (cred->cr_uidinfo->ui_uid == uid) {
1375 uip = cred->cr_uidinfo;
1378 } else if (cred->cr_ruidinfo->ui_uid == uid) {
1379 uip = cred->cr_ruidinfo;
1384 rw_rlock(&uihashtbl_lock);
1385 uip = uilookup(uid);
1386 rw_runlock(&uihashtbl_lock);
1390 new_uip = malloc(sizeof(*new_uip), M_UIDINFO, M_WAITOK | M_ZERO);
1391 racct_create(&new_uip->ui_racct);
1392 refcount_init(&new_uip->ui_ref, 1);
1393 new_uip->ui_uid = uid;
1395 rw_wlock(&uihashtbl_lock);
1397 * There's a chance someone created our uidinfo while we
1398 * were in malloc and not holding the lock, so we have to
1399 * make sure we don't insert a duplicate uidinfo.
1401 if ((uip = uilookup(uid)) == NULL) {
1402 LIST_INSERT_HEAD(UIHASH(uid), new_uip, ui_hash);
1403 rw_wunlock(&uihashtbl_lock);
1406 rw_wunlock(&uihashtbl_lock);
1407 racct_destroy(&new_uip->ui_racct);
1408 free(new_uip, M_UIDINFO);
1414 * Place another refcount on a uidinfo struct.
1417 uihold(struct uidinfo *uip)
1420 refcount_acquire(&uip->ui_ref);
1424 * Since uidinfo structs have a long lifetime, we use an
1425 * opportunistic refcounting scheme to avoid locking the lookup hash
1428 * If the refcount hits 0, we need to free the structure,
1429 * which means we need to lock the hash.
1431 * After locking the struct and lowering the refcount, if we find
1432 * that we don't need to free, simply unlock and return.
1434 * If refcount lowering results in need to free, bump the count
1435 * back up, lose the lock and acquire the locks in the proper
1436 * order to try again.
1439 uifree(struct uidinfo *uip)
1442 if (refcount_release_if_not_last(&uip->ui_ref))
1445 rw_wlock(&uihashtbl_lock);
1446 if (refcount_release(&uip->ui_ref) == 0) {
1447 rw_wunlock(&uihashtbl_lock);
1451 racct_destroy(&uip->ui_racct);
1452 LIST_REMOVE(uip, ui_hash);
1453 rw_wunlock(&uihashtbl_lock);
1455 if (uip->ui_sbsize != 0)
1456 printf("freeing uidinfo: uid = %d, sbsize = %ld\n",
1457 uip->ui_uid, uip->ui_sbsize);
1458 if (uip->ui_proccnt != 0)
1459 printf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1460 uip->ui_uid, uip->ui_proccnt);
1461 if (uip->ui_vmsize != 0)
1462 printf("freeing uidinfo: uid = %d, swapuse = %lld\n",
1463 uip->ui_uid, (unsigned long long)uip->ui_vmsize);
1464 free(uip, M_UIDINFO);
1469 ui_racct_foreach(void (*callback)(struct racct *racct,
1470 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
1471 void *arg2, void *arg3)
1473 struct uidinfo *uip;
1474 struct uihashhead *uih;
1476 rw_rlock(&uihashtbl_lock);
1479 for (uih = &uihashtbl[uihash]; uih >= uihashtbl; uih--) {
1480 LIST_FOREACH(uip, uih, ui_hash) {
1481 (callback)(uip->ui_racct, arg2, arg3);
1486 rw_runlock(&uihashtbl_lock);
1491 chglimit(struct uidinfo *uip, long *limit, int diff, rlim_t max, const char *name)
1495 /* Don't allow them to exceed max, but allow subtraction. */
1496 new = atomic_fetchadd_long(limit, (long)diff) + diff;
1497 if (diff > 0 && max != 0) {
1498 if (new < 0 || new > max) {
1499 atomic_subtract_long(limit, (long)diff);
1503 printf("negative %s for uid = %d\n", name, uip->ui_uid);
1508 * Change the count associated with number of processes
1509 * a given user is using. When 'max' is 0, don't enforce a limit
1512 chgproccnt(struct uidinfo *uip, int diff, rlim_t max)
1515 return (chglimit(uip, &uip->ui_proccnt, diff, max, "proccnt"));
1519 * Change the total socket buffer size a user has used.
1522 chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to, rlim_t max)
1527 if (diff > 0 && max == 0) {
1530 rv = chglimit(uip, &uip->ui_sbsize, diff, max, "sbsize");
1538 * Change the count associated with number of pseudo-terminals
1539 * a given user is using. When 'max' is 0, don't enforce a limit
1542 chgptscnt(struct uidinfo *uip, int diff, rlim_t max)
1545 return (chglimit(uip, &uip->ui_ptscnt, diff, max, "ptscnt"));
1549 chgkqcnt(struct uidinfo *uip, int diff, rlim_t max)
1552 return (chglimit(uip, &uip->ui_kqcnt, diff, max, "kqcnt"));
1556 chgumtxcnt(struct uidinfo *uip, int diff, rlim_t max)
1559 return (chglimit(uip, &uip->ui_umtxcnt, diff, max, "umtxcnt"));