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. 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
35 * $FreeBSD: src/sys/kern/kern_resource.c,v 1.55.2.5 2001/11/03 01:41:08 ps Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
42 #include <sys/kern_syscall.h>
43 #include <sys/kernel.h>
44 #include <sys/resourcevar.h>
45 #include <sys/malloc.h>
49 #include <sys/lockf.h>
52 #include <vm/vm_param.h>
55 #include <vm/vm_map.h>
57 #include <sys/thread2.h>
58 #include <sys/spinlock2.h>
60 static int donice (struct proc *chgp, int n);
61 static int doionice (struct proc *chgp, int n);
63 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
64 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
65 static struct spinlock uihash_lock;
66 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
67 static u_long uihash; /* size of hash table - 1 */
69 static struct uidinfo *uicreate (uid_t uid);
70 static struct uidinfo *uilookup (uid_t uid);
73 * Resource controls and accounting.
76 struct getpriority_info {
81 static int getpriority_callback(struct proc *p, void *data);
87 sys_getpriority(struct getpriority_args *uap)
89 struct getpriority_info info;
90 thread_t curtd = curthread;
91 struct proc *curp = curproc;
94 int low = PRIO_MAX + 1;
104 lwkt_gettoken_shared(&p->p_token);
105 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred))
107 lwkt_reltoken(&p->p_token);
114 lwkt_gettoken_shared(&curp->p_token);
117 lwkt_reltoken(&curp->p_token);
118 } else if ((pg = pgfind(uap->who)) == NULL) {
120 } /* else ref held from pgfind */
122 lwkt_gettoken_shared(&pg->pg_token);
123 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
124 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred) &&
129 lwkt_reltoken(&pg->pg_token);
134 uap->who = curtd->td_ucred->cr_uid;
137 allproc_scan(getpriority_callback, &info);
145 if (low == PRIO_MAX + 1) {
149 uap->sysmsg_result = low;
156 * Figure out the current lowest nice priority for processes owned
157 * by the specified user.
161 getpriority_callback(struct proc *p, void *data)
163 struct getpriority_info *info = data;
165 lwkt_gettoken_shared(&p->p_token);
166 if (PRISON_CHECK(curthread->td_ucred, p->p_ucred) &&
167 p->p_ucred->cr_uid == info->who &&
168 p->p_nice < info->low) {
169 info->low = p->p_nice;
171 lwkt_reltoken(&p->p_token);
175 struct setpriority_info {
182 static int setpriority_callback(struct proc *p, void *data);
188 sys_setpriority(struct setpriority_args *uap)
190 struct setpriority_info info;
191 thread_t curtd = curthread;
192 struct proc *curp = curproc;
195 int found = 0, error = 0;
197 switch (uap->which) {
200 lwkt_gettoken(&curp->p_token);
201 error = donice(curp, uap->prio);
203 lwkt_reltoken(&curp->p_token);
207 lwkt_gettoken(&p->p_token);
208 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred)) {
209 error = donice(p, uap->prio);
212 lwkt_reltoken(&p->p_token);
219 lwkt_gettoken_shared(&curp->p_token);
222 lwkt_reltoken(&curp->p_token);
223 } else if ((pg = pgfind(uap->who)) == NULL) {
225 } /* else ref held from pgfind */
227 lwkt_gettoken(&pg->pg_token);
229 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
231 lwkt_gettoken(&p->p_token);
232 if (p->p_pgrp == pg &&
233 PRISON_CHECK(curtd->td_ucred, p->p_ucred)) {
234 error = donice(p, uap->prio);
237 lwkt_reltoken(&p->p_token);
238 if (p->p_pgrp != pg) {
244 lwkt_reltoken(&pg->pg_token);
249 uap->who = curtd->td_ucred->cr_uid;
250 info.prio = uap->prio;
254 allproc_scan(setpriority_callback, &info);
271 setpriority_callback(struct proc *p, void *data)
273 struct setpriority_info *info = data;
276 lwkt_gettoken(&p->p_token);
277 if (p->p_ucred->cr_uid == info->who &&
278 PRISON_CHECK(curthread->td_ucred, p->p_ucred)) {
279 error = donice(p, info->prio);
284 lwkt_reltoken(&p->p_token);
289 * Caller must hold chgp->p_token
292 donice(struct proc *chgp, int n)
294 struct ucred *cr = curthread->td_ucred;
297 if (cr->cr_uid && cr->cr_ruid &&
298 cr->cr_uid != chgp->p_ucred->cr_uid &&
299 cr->cr_ruid != chgp->p_ucred->cr_uid)
305 if (n < chgp->p_nice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
308 FOREACH_LWP_IN_PROC(lp, chgp) {
310 chgp->p_usched->resetpriority(lp);
317 struct ioprio_get_info {
322 static int ioprio_get_callback(struct proc *p, void *data);
328 sys_ioprio_get(struct ioprio_get_args *uap)
330 struct ioprio_get_info info;
331 thread_t curtd = curthread;
332 struct proc *curp = curproc;
335 int high = IOPRIO_MIN-2;
338 switch (uap->which) {
341 high = curp->p_ionice;
345 lwkt_gettoken_shared(&p->p_token);
346 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred))
348 lwkt_reltoken(&p->p_token);
355 lwkt_gettoken_shared(&curp->p_token);
358 lwkt_reltoken(&curp->p_token);
359 } else if ((pg = pgfind(uap->who)) == NULL) {
361 } /* else ref held from pgfind */
363 lwkt_gettoken_shared(&pg->pg_token);
364 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
365 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred) &&
369 lwkt_reltoken(&pg->pg_token);
374 uap->who = curtd->td_ucred->cr_uid;
377 allproc_scan(ioprio_get_callback, &info);
384 if (high == IOPRIO_MIN-2) {
388 uap->sysmsg_result = high;
395 * Figure out the current lowest nice priority for processes owned
396 * by the specified user.
400 ioprio_get_callback(struct proc *p, void *data)
402 struct ioprio_get_info *info = data;
404 lwkt_gettoken_shared(&p->p_token);
405 if (PRISON_CHECK(curthread->td_ucred, p->p_ucred) &&
406 p->p_ucred->cr_uid == info->who &&
407 p->p_ionice > info->high) {
408 info->high = p->p_ionice;
410 lwkt_reltoken(&p->p_token);
415 struct ioprio_set_info {
422 static int ioprio_set_callback(struct proc *p, void *data);
428 sys_ioprio_set(struct ioprio_set_args *uap)
430 struct ioprio_set_info info;
431 thread_t curtd = curthread;
432 struct proc *curp = curproc;
435 int found = 0, error = 0;
437 switch (uap->which) {
440 lwkt_gettoken(&curp->p_token);
441 error = doionice(curp, uap->prio);
442 lwkt_reltoken(&curp->p_token);
447 lwkt_gettoken(&p->p_token);
448 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred)) {
449 error = doionice(p, uap->prio);
452 lwkt_reltoken(&p->p_token);
459 lwkt_gettoken_shared(&curp->p_token);
462 lwkt_reltoken(&curp->p_token);
463 } else if ((pg = pgfind(uap->who)) == NULL) {
465 } /* else ref held from pgfind */
467 lwkt_gettoken(&pg->pg_token);
469 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
471 lwkt_gettoken(&p->p_token);
472 if (p->p_pgrp == pg &&
473 PRISON_CHECK(curtd->td_ucred, p->p_ucred)) {
474 error = doionice(p, uap->prio);
477 lwkt_reltoken(&p->p_token);
478 if (p->p_pgrp != pg) {
484 lwkt_reltoken(&pg->pg_token);
489 uap->who = curtd->td_ucred->cr_uid;
490 info.prio = uap->prio;
494 allproc_scan(ioprio_set_callback, &info);
511 ioprio_set_callback(struct proc *p, void *data)
513 struct ioprio_set_info *info = data;
516 lwkt_gettoken(&p->p_token);
517 if (p->p_ucred->cr_uid == info->who &&
518 PRISON_CHECK(curthread->td_ucred, p->p_ucred)) {
519 error = doionice(p, info->prio);
524 lwkt_reltoken(&p->p_token);
529 doionice(struct proc *chgp, int n)
531 struct ucred *cr = curthread->td_ucred;
533 if (cr->cr_uid && cr->cr_ruid &&
534 cr->cr_uid != chgp->p_ucred->cr_uid &&
535 cr->cr_ruid != chgp->p_ucred->cr_uid)
541 if (n < chgp->p_ionice &&
542 priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
554 sys_lwp_rtprio(struct lwp_rtprio_args *uap)
556 struct ucred *cr = curthread->td_ucred;
562 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
578 lwkt_gettoken(&p->p_token);
584 if (uap->tid == -1) {
586 * sadly, tid can be 0 so we can't use 0 here
589 lp = curthread->td_lwp;
591 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
599 * Make sure that this lwp is not ripped if any of the following
600 * code blocks, e.g. copyout.
603 switch (uap->function) {
605 error = copyout(&lp->lwp_rtprio, uap->rtp,
606 sizeof(struct rtprio));
609 if (cr->cr_uid && cr->cr_ruid &&
610 cr->cr_uid != p->p_ucred->cr_uid &&
611 cr->cr_ruid != p->p_ucred->cr_uid) {
615 /* disallow setting rtprio in most cases if not superuser */
616 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
617 /* can't set someone else's */
618 if (uap->pid) { /* XXX */
622 /* can't set realtime priority */
624 * Realtime priority has to be restricted for reasons which should be
625 * obvious. However, for idle priority, there is a potential for
626 * system deadlock if an idleprio process gains a lock on a resource
627 * that other processes need (and the idleprio process can't run
628 * due to a CPU-bound normal process). Fix me! XXX
630 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
639 case RTP_PRIO_REALTIME:
640 case RTP_PRIO_NORMAL:
642 if (rtp.prio > RTP_PRIO_MAX) {
645 lp->lwp_rtprio = rtp;
662 lwkt_reltoken(&p->p_token);
669 * Set realtime priority
674 sys_rtprio(struct rtprio_args *uap)
676 struct ucred *cr = curthread->td_ucred;
682 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
697 lwkt_gettoken(&p->p_token);
700 lp = FIRST_LWP_IN_PROC(p);
701 switch (uap->function) {
703 error = copyout(&lp->lwp_rtprio, uap->rtp,
704 sizeof(struct rtprio));
707 if (cr->cr_uid && cr->cr_ruid &&
708 cr->cr_uid != p->p_ucred->cr_uid &&
709 cr->cr_ruid != p->p_ucred->cr_uid) {
713 /* disallow setting rtprio in most cases if not superuser */
714 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
715 /* can't set someone else's */
720 /* can't set realtime priority */
722 * Realtime priority has to be restricted for reasons which should be
723 * obvious. However, for idle priority, there is a potential for
724 * system deadlock if an idleprio process gains a lock on a resource
725 * that other processes need (and the idleprio process can't run
726 * due to a CPU-bound normal process). Fix me! XXX
728 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
737 case RTP_PRIO_REALTIME:
738 case RTP_PRIO_NORMAL:
740 if (rtp.prio > RTP_PRIO_MAX) {
744 lp->lwp_rtprio = rtp;
758 lwkt_reltoken(&p->p_token);
769 sys_setrlimit(struct __setrlimit_args *uap)
774 error = copyin(uap->rlp, &alim, sizeof(alim));
778 error = kern_setrlimit(uap->which, &alim);
787 sys_getrlimit(struct __getrlimit_args *uap)
792 error = kern_getrlimit(uap->which, &lim);
795 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
800 * Transform the running time and tick information in lwp lp's thread into user,
801 * system, and interrupt time usage.
803 * Since we are limited to statclock tick granularity this is a statisical
804 * calculation which will be correct over the long haul, but should not be
805 * expected to measure fine grained deltas.
807 * It is possible to catch a lwp in the midst of being created, so
808 * check whether lwp_thread is NULL or not.
811 calcru(struct lwp *lp, struct timeval *up, struct timeval *sp)
816 * Calculate at the statclock level. YYY if the thread is owned by
817 * another cpu we need to forward the request to the other cpu, or
818 * have a token to interlock the information in order to avoid racing
819 * thread destruction.
821 if ((td = lp->lwp_thread) != NULL) {
823 up->tv_sec = td->td_uticks / 1000000;
824 up->tv_usec = td->td_uticks % 1000000;
825 sp->tv_sec = td->td_sticks / 1000000;
826 sp->tv_usec = td->td_sticks % 1000000;
832 * Aggregate resource statistics of all lwps of a process.
834 * proc.p_ru keeps track of all statistics directly related to a proc. This
835 * consists of RSS usage and nswap information and aggregate numbers for all
836 * former lwps of this proc.
838 * proc.p_cru is the sum of all stats of reaped children.
840 * lwp.lwp_ru contains the stats directly related to one specific lwp, meaning
841 * packet, scheduler switch or page fault counts, etc. This information gets
842 * added to lwp.lwp_proc.p_ru when the lwp exits.
845 calcru_proc(struct proc *p, struct rusage *ru)
847 struct timeval upt, spt;
853 FOREACH_LWP_IN_PROC(lp, p) {
854 calcru(lp, &upt, &spt);
855 timevaladd(&ru->ru_utime, &upt);
856 timevaladd(&ru->ru_stime, &spt);
857 for (rip1 = &ru->ru_first, rip2 = &lp->lwp_ru.ru_first;
858 rip1 <= &ru->ru_last;
869 sys_getrusage(struct getrusage_args *uap)
871 struct proc *p = curproc;
876 lwkt_gettoken(&p->p_token);
884 case RUSAGE_CHILDREN:
892 lwkt_reltoken(&p->p_token);
895 error = copyout(rup, uap->rusage, sizeof(struct rusage));
900 ruadd(struct rusage *ru, struct rusage *ru2)
905 timevaladd(&ru->ru_utime, &ru2->ru_utime);
906 timevaladd(&ru->ru_stime, &ru2->ru_stime);
907 if (ru->ru_maxrss < ru2->ru_maxrss)
908 ru->ru_maxrss = ru2->ru_maxrss;
909 ip = &ru->ru_first; ip2 = &ru2->ru_first;
910 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
915 * Find the uidinfo structure for a uid. This structure is used to
916 * track the total resource consumption (process count, socket buffer
917 * size, etc.) for the uid and impose limits.
922 spin_init(&uihash_lock, "uihashinit");
923 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
927 * NOTE: Must be called with uihash_lock held
931 static struct uidinfo *
934 struct uihashhead *uipp;
938 LIST_FOREACH(uip, uipp, ui_hash) {
939 if (uip->ui_uid == uid)
946 * Helper function to creat ea uid that could not be found.
947 * This function will properly deal with races.
951 static struct uidinfo *
954 struct uidinfo *uip, *tmp;
957 * Allocate space and check for a race
959 uip = kmalloc(sizeof(*uip), M_UIDINFO, M_WAITOK|M_ZERO);
962 * Initialize structure and enter it into the hash table
964 spin_init(&uip->ui_lock, "uicreate");
966 uip->ui_ref = 1; /* we're returning a ref */
967 varsymset_init(&uip->ui_varsymset, NULL);
970 * Somebody may have already created the uidinfo for this
971 * uid. If so, return that instead.
973 spin_lock(&uihash_lock);
977 spin_unlock(&uihash_lock);
979 spin_uninit(&uip->ui_lock);
980 varsymset_clean(&uip->ui_varsymset);
981 kfree(uip, M_UIDINFO);
984 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
985 spin_unlock(&uihash_lock);
1000 spin_lock(&uihash_lock);
1001 uip = uilookup(uid);
1003 spin_unlock(&uihash_lock);
1004 uip = uicreate(uid);
1007 spin_unlock(&uihash_lock);
1013 * Helper funtion to remove a uidinfo whos reference count is
1014 * transitioning from 1->0. The reference count is 1 on call.
1016 * Zero is returned on success, otherwise non-zero and the
1017 * uiphas not been removed.
1022 uifree(struct uidinfo *uip)
1025 * If we are still the only holder after acquiring the uihash_lock
1026 * we can safely unlink the uip and destroy it. Otherwise we lost
1027 * a race and must fail.
1029 spin_lock(&uihash_lock);
1030 if (uip->ui_ref != 1) {
1031 spin_unlock(&uihash_lock);
1034 LIST_REMOVE(uip, ui_hash);
1035 spin_unlock(&uihash_lock);
1038 * The uip is now orphaned and we can destroy it at our
1041 if (uip->ui_sbsize != 0)
1042 kprintf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1043 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1044 if (uip->ui_proccnt != 0)
1045 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1046 uip->ui_uid, uip->ui_proccnt);
1048 varsymset_clean(&uip->ui_varsymset);
1049 lockuninit(&uip->ui_varsymset.vx_lock);
1050 spin_uninit(&uip->ui_lock);
1051 kfree(uip, M_UIDINFO);
1059 uihold(struct uidinfo *uip)
1061 atomic_add_int(&uip->ui_ref, 1);
1062 KKASSERT(uip->ui_ref >= 0);
1066 * NOTE: It is important for us to not drop the ref count to 0
1067 * because this can cause a 2->0/2->0 race with another
1068 * concurrent dropper. Losing the race in that situation
1069 * can cause uip to become stale for one of the other
1075 uidrop(struct uidinfo *uip)
1079 KKASSERT(uip->ui_ref > 0);
1085 if (uifree(uip) == 0)
1087 } else if (atomic_cmpset_int(&uip->ui_ref, ref, ref - 1)) {
1095 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
1102 * Change the count associated with number of processes
1103 * a given user is using. When 'max' is 0, don't enforce a limit
1106 chgproccnt(struct uidinfo *uip, int diff, int max)
1109 spin_lock(&uip->ui_lock);
1110 /* don't allow them to exceed max, but allow subtraction */
1111 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1114 uip->ui_proccnt += diff;
1115 if (uip->ui_proccnt < 0)
1116 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
1119 spin_unlock(&uip->ui_lock);
1124 * Change the total socket buffer size a user has used.
1127 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
1134 sbsize = atomic_fetchadd_long(&uip->ui_sbsize, to - *hiwat);
1135 new = sbsize + to - *hiwat;
1137 spin_lock(&uip->ui_lock);
1138 new = uip->ui_sbsize + to - *hiwat;
1139 uip->ui_sbsize = new;
1140 spin_unlock(&uip->ui_lock);
1145 * If we are trying to increase the socket buffer size
1146 * Scale down the hi water mark when we exceed the user's
1147 * allowed socket buffer space.
1149 * We can't scale down too much or we will blow up atomic packet
1152 if (to > *hiwat && to > MCLBYTES && new > max) {
1153 to = to * max / new;