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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_resource.c,v 1.55.2.5 2001/11/03 01:41:08 ps Exp $
42 #include "opt_compat.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
48 #include <sys/kern_syscall.h>
49 #include <sys/kernel.h>
50 #include <sys/resourcevar.h>
51 #include <sys/malloc.h>
55 #include <sys/lockf.h>
58 #include <vm/vm_param.h>
61 #include <vm/vm_map.h>
63 #include <sys/thread2.h>
64 #include <sys/spinlock2.h>
66 static int donice (struct proc *chgp, int n);
67 static int doionice (struct proc *chgp, int n);
69 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
70 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
71 static struct spinlock uihash_lock;
72 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
73 static u_long uihash; /* size of hash table - 1 */
75 static struct uidinfo *uicreate (uid_t uid);
76 static struct uidinfo *uilookup (uid_t uid);
79 * Resource controls and accounting.
82 struct getpriority_info {
87 static int getpriority_callback(struct proc *p, void *data);
93 sys_getpriority(struct getpriority_args *uap)
95 struct getpriority_info info;
96 struct proc *curp = curproc;
98 int low = PRIO_MAX + 1;
101 switch (uap->which) {
110 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
124 } else if ((pg = pgfind(uap->who)) == NULL) {
126 } /* else ref held from pgfind */
128 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
129 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
139 uap->who = curp->p_ucred->cr_uid;
142 allproc_scan(getpriority_callback, &info);
150 if (low == PRIO_MAX + 1) {
154 uap->sysmsg_result = low;
161 * Figure out the current lowest nice priority for processes owned
162 * by the specified user.
166 getpriority_callback(struct proc *p, void *data)
168 struct getpriority_info *info = data;
170 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
171 p->p_ucred->cr_uid == info->who &&
172 p->p_nice < info->low) {
173 info->low = p->p_nice;
178 struct setpriority_info {
185 static int setpriority_callback(struct proc *p, void *data);
191 sys_setpriority(struct setpriority_args *uap)
193 struct setpriority_info info;
194 struct proc *curp = curproc;
196 int found = 0, error = 0;
198 lwkt_gettoken(&proc_token);
200 switch (uap->which) {
209 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
210 error = donice(p, uap->prio);
224 } else if ((pg = pgfind(uap->who)) == NULL) {
226 } /* else ref held from pgfind */
228 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
229 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
230 error = donice(p, uap->prio);
239 uap->who = curp->p_ucred->cr_uid;
240 info.prio = uap->prio;
244 allproc_scan(setpriority_callback, &info);
255 lwkt_reltoken(&proc_token);
264 setpriority_callback(struct proc *p, void *data)
266 struct setpriority_info *info = data;
269 if (p->p_ucred->cr_uid == info->who &&
270 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
271 error = donice(p, info->prio);
280 donice(struct proc *chgp, int n)
282 struct proc *curp = curproc;
283 struct ucred *cr = curp->p_ucred;
286 if (cr->cr_uid && cr->cr_ruid &&
287 cr->cr_uid != chgp->p_ucred->cr_uid &&
288 cr->cr_ruid != chgp->p_ucred->cr_uid)
294 if (n < chgp->p_nice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
297 FOREACH_LWP_IN_PROC(lp, chgp) {
299 chgp->p_usched->resetpriority(lp);
306 struct ioprio_get_info {
311 static int ioprio_get_callback(struct proc *p, void *data);
317 sys_ioprio_get(struct ioprio_get_args *uap)
319 struct ioprio_get_info info;
320 struct proc *curp = curproc;
322 int high = IOPRIO_MIN-2;
325 lwkt_gettoken(&proc_token);
327 switch (uap->which) {
336 if (PRISON_CHECK(curp->p_ucred, p->p_ucred))
349 } else if ((pg = pgfind(uap->who)) == NULL) {
351 } /* else ref held from pgfind */
353 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
354 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
363 uap->who = curp->p_ucred->cr_uid;
366 allproc_scan(ioprio_get_callback, &info);
374 if (high == IOPRIO_MIN-2) {
378 uap->sysmsg_result = high;
381 lwkt_reltoken(&proc_token);
387 * Figure out the current lowest nice priority for processes owned
388 * by the specified user.
392 ioprio_get_callback(struct proc *p, void *data)
394 struct ioprio_get_info *info = data;
396 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
397 p->p_ucred->cr_uid == info->who &&
398 p->p_ionice > info->high) {
399 info->high = p->p_ionice;
405 struct ioprio_set_info {
412 static int ioprio_set_callback(struct proc *p, void *data);
418 sys_ioprio_set(struct ioprio_set_args *uap)
420 struct ioprio_set_info info;
421 struct proc *curp = curproc;
423 int found = 0, error = 0;
425 lwkt_gettoken(&proc_token);
427 switch (uap->which) {
436 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
437 error = doionice(p, uap->prio);
451 } else if ((pg = pgfind(uap->who)) == NULL) {
453 } /* else ref held from pgfind */
455 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
456 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
457 error = doionice(p, uap->prio);
466 uap->who = curp->p_ucred->cr_uid;
467 info.prio = uap->prio;
471 allproc_scan(ioprio_set_callback, &info);
482 lwkt_reltoken(&proc_token);
491 ioprio_set_callback(struct proc *p, void *data)
493 struct ioprio_set_info *info = data;
496 if (p->p_ucred->cr_uid == info->who &&
497 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
498 error = doionice(p, info->prio);
507 doionice(struct proc *chgp, int n)
509 struct proc *curp = curproc;
510 struct ucred *cr = curp->p_ucred;
512 if (cr->cr_uid && cr->cr_ruid &&
513 cr->cr_uid != chgp->p_ucred->cr_uid &&
514 cr->cr_ruid != chgp->p_ucred->cr_uid)
520 if (n < chgp->p_ionice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
532 sys_lwp_rtprio(struct lwp_rtprio_args *uap)
537 struct ucred *cr = curthread->td_ucred;
540 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
546 lwkt_gettoken(&proc_token);
564 if (uap->tid == -1) {
566 * sadly, tid can be 0 so we can't use 0 here
569 lp = curthread->td_lwp;
571 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
578 switch (uap->function) {
580 error = copyout(&lp->lwp_rtprio, uap->rtp,
581 sizeof(struct rtprio));
584 if (cr->cr_uid && cr->cr_ruid &&
585 cr->cr_uid != p->p_ucred->cr_uid &&
586 cr->cr_ruid != p->p_ucred->cr_uid) {
590 /* disallow setting rtprio in most cases if not superuser */
591 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
592 /* can't set someone else's */
593 if (uap->pid) { /* XXX */
597 /* can't set realtime priority */
599 * Realtime priority has to be restricted for reasons which should be
600 * obvious. However, for idle priority, there is a potential for
601 * system deadlock if an idleprio process gains a lock on a resource
602 * that other processes need (and the idleprio process can't run
603 * due to a CPU-bound normal process). Fix me! XXX
605 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
614 case RTP_PRIO_REALTIME:
615 case RTP_PRIO_NORMAL:
617 if (rtp.prio > RTP_PRIO_MAX) {
620 lp->lwp_rtprio = rtp;
637 lwkt_reltoken(&proc_token);
643 * Set realtime priority
648 sys_rtprio(struct rtprio_args *uap)
652 struct ucred *cr = curthread->td_ucred;
656 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
660 lwkt_gettoken(&proc_token);
675 lp = FIRST_LWP_IN_PROC(p);
676 switch (uap->function) {
678 error = copyout(&lp->lwp_rtprio, uap->rtp,
679 sizeof(struct rtprio));
682 if (cr->cr_uid && cr->cr_ruid &&
683 cr->cr_uid != p->p_ucred->cr_uid &&
684 cr->cr_ruid != p->p_ucred->cr_uid) {
688 /* disallow setting rtprio in most cases if not superuser */
689 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
690 /* can't set someone else's */
695 /* can't set realtime priority */
697 * Realtime priority has to be restricted for reasons which should be
698 * obvious. However, for idle priority, there is a potential for
699 * system deadlock if an idleprio process gains a lock on a resource
700 * that other processes need (and the idleprio process can't run
701 * due to a CPU-bound normal process). Fix me! XXX
703 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
712 case RTP_PRIO_REALTIME:
713 case RTP_PRIO_NORMAL:
715 if (rtp.prio > RTP_PRIO_MAX) {
719 lp->lwp_rtprio = rtp;
734 lwkt_reltoken(&proc_token);
743 sys_setrlimit(struct __setrlimit_args *uap)
748 error = copyin(uap->rlp, &alim, sizeof(alim));
752 error = kern_setrlimit(uap->which, &alim);
761 sys_getrlimit(struct __getrlimit_args *uap)
766 error = kern_getrlimit(uap->which, &lim);
769 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
774 * Transform the running time and tick information in lwp lp's thread into user,
775 * system, and interrupt time usage.
777 * Since we are limited to statclock tick granularity this is a statisical
778 * calculation which will be correct over the long haul, but should not be
779 * expected to measure fine grained deltas.
781 * It is possible to catch a lwp in the midst of being created, so
782 * check whether lwp_thread is NULL or not.
785 calcru(struct lwp *lp, struct timeval *up, struct timeval *sp)
790 * Calculate at the statclock level. YYY if the thread is owned by
791 * another cpu we need to forward the request to the other cpu, or
792 * have a token to interlock the information in order to avoid racing
793 * thread destruction.
795 if ((td = lp->lwp_thread) != NULL) {
797 up->tv_sec = td->td_uticks / 1000000;
798 up->tv_usec = td->td_uticks % 1000000;
799 sp->tv_sec = td->td_sticks / 1000000;
800 sp->tv_usec = td->td_sticks % 1000000;
806 * Aggregate resource statistics of all lwps of a process.
808 * proc.p_ru keeps track of all statistics directly related to a proc. This
809 * consists of RSS usage and nswap information and aggregate numbers for all
810 * former lwps of this proc.
812 * proc.p_cru is the sum of all stats of reaped children.
814 * lwp.lwp_ru contains the stats directly related to one specific lwp, meaning
815 * packet, scheduler switch or page fault counts, etc. This information gets
816 * added to lwp.lwp_proc.p_ru when the lwp exits.
819 calcru_proc(struct proc *p, struct rusage *ru)
821 struct timeval upt, spt;
827 FOREACH_LWP_IN_PROC(lp, p) {
828 calcru(lp, &upt, &spt);
829 timevaladd(&ru->ru_utime, &upt);
830 timevaladd(&ru->ru_stime, &spt);
831 for (rip1 = &ru->ru_first, rip2 = &lp->lwp_ru.ru_first;
832 rip1 <= &ru->ru_last;
843 sys_getrusage(struct getrusage_args *uap)
849 lwkt_gettoken(&proc_token);
854 calcru_proc(curproc, rup);
857 case RUSAGE_CHILDREN:
858 rup = &curproc->p_cru;
866 error = copyout(rup, uap->rusage, sizeof(struct rusage));
867 lwkt_reltoken(&proc_token);
872 ruadd(struct rusage *ru, struct rusage *ru2)
877 timevaladd(&ru->ru_utime, &ru2->ru_utime);
878 timevaladd(&ru->ru_stime, &ru2->ru_stime);
879 if (ru->ru_maxrss < ru2->ru_maxrss)
880 ru->ru_maxrss = ru2->ru_maxrss;
881 ip = &ru->ru_first; ip2 = &ru2->ru_first;
882 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
887 * Find the uidinfo structure for a uid. This structure is used to
888 * track the total resource consumption (process count, socket buffer
889 * size, etc.) for the uid and impose limits.
894 spin_init(&uihash_lock);
895 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
899 * NOTE: Must be called with uihash_lock held
903 static struct uidinfo *
906 struct uihashhead *uipp;
910 LIST_FOREACH(uip, uipp, ui_hash) {
911 if (uip->ui_uid == uid)
918 * Helper function to creat ea uid that could not be found.
919 * This function will properly deal with races.
923 static struct uidinfo *
926 struct uidinfo *uip, *tmp;
929 * Allocate space and check for a race
931 uip = kmalloc(sizeof(*uip), M_UIDINFO, M_WAITOK|M_ZERO);
934 * Initialize structure and enter it into the hash table
936 spin_init(&uip->ui_lock);
938 uip->ui_ref = 1; /* we're returning a ref */
939 varsymset_init(&uip->ui_varsymset, NULL);
942 * Somebody may have already created the uidinfo for this
943 * uid. If so, return that instead.
945 spin_lock(&uihash_lock);
949 spin_unlock(&uihash_lock);
951 spin_uninit(&uip->ui_lock);
952 varsymset_clean(&uip->ui_varsymset);
953 kfree(uip, M_UIDINFO);
956 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
957 spin_unlock(&uihash_lock);
972 spin_lock(&uihash_lock);
975 spin_unlock(&uihash_lock);
979 spin_unlock(&uihash_lock);
985 * Helper funtion to remove a uidinfo whos reference count is
986 * transitioning from 1->0. The reference count is 1 on call.
988 * Zero is returned on success, otherwise non-zero and the
989 * uiphas not been removed.
994 uifree(struct uidinfo *uip)
997 * If we are still the only holder after acquiring the uihash_lock
998 * we can safely unlink the uip and destroy it. Otherwise we lost
999 * a race and must fail.
1001 spin_lock(&uihash_lock);
1002 if (uip->ui_ref != 1) {
1003 spin_unlock(&uihash_lock);
1006 LIST_REMOVE(uip, ui_hash);
1007 spin_unlock(&uihash_lock);
1010 * The uip is now orphaned and we can destroy it at our
1013 if (uip->ui_sbsize != 0)
1014 kprintf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1015 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1016 if (uip->ui_proccnt != 0)
1017 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1018 uip->ui_uid, uip->ui_proccnt);
1020 varsymset_clean(&uip->ui_varsymset);
1021 lockuninit(&uip->ui_varsymset.vx_lock);
1022 spin_uninit(&uip->ui_lock);
1023 kfree(uip, M_UIDINFO);
1031 uihold(struct uidinfo *uip)
1033 atomic_add_int(&uip->ui_ref, 1);
1034 KKASSERT(uip->ui_ref >= 0);
1038 * NOTE: It is important for us to not drop the ref count to 0
1039 * because this can cause a 2->0/2->0 race with another
1040 * concurrent dropper. Losing the race in that situation
1041 * can cause uip to become stale for one of the other
1047 uidrop(struct uidinfo *uip)
1051 KKASSERT(uip->ui_ref > 0);
1057 if (uifree(uip) == 0)
1059 } else if (atomic_cmpset_int(&uip->ui_ref, ref, ref - 1)) {
1067 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
1074 * Change the count associated with number of processes
1075 * a given user is using. When 'max' is 0, don't enforce a limit
1078 chgproccnt(struct uidinfo *uip, int diff, int max)
1081 spin_lock(&uip->ui_lock);
1082 /* don't allow them to exceed max, but allow subtraction */
1083 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1086 uip->ui_proccnt += diff;
1087 if (uip->ui_proccnt < 0)
1088 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
1091 spin_unlock(&uip->ui_lock);
1096 * Change the total socket buffer size a user has used.
1099 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
1103 spin_lock(&uip->ui_lock);
1104 new = uip->ui_sbsize + to - *hiwat;
1108 * If we are trying to increase the socket buffer size
1109 * Scale down the hi water mark when we exceed the user's
1110 * allowed socket buffer space.
1112 * We can't scale down too much or we will blow up atomic packet
1115 if (to > *hiwat && to > MCLBYTES && new > max) {
1116 to = to * max / new;
1120 uip->ui_sbsize = new;
1122 spin_unlock(&uip->ui_lock);