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/sysmsg.h>
42 #include <sys/kernel.h>
43 #include <sys/resourcevar.h>
44 #include <sys/malloc.h>
48 #include <sys/lockf.h>
51 #include <vm/vm_param.h>
54 #include <vm/vm_map.h>
56 #include <sys/thread2.h>
57 #include <sys/spinlock2.h>
59 static int donice (struct proc *chgp, int n);
60 static int doionice (struct proc *chgp, int n);
62 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
63 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
64 static struct spinlock uihash_lock;
65 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
66 static u_long uihash; /* size of hash table - 1 */
68 static struct uidinfo *uilookup (uid_t uid);
71 * Resource controls and accounting.
74 struct getpriority_info {
79 static int getpriority_callback(struct proc *p, void *data);
85 sys_getpriority(struct sysmsg *sysmsg, const struct getpriority_args *uap)
87 struct getpriority_info info;
88 thread_t curtd = curthread;
89 struct proc *curp = curproc;
92 int low = PRIO_MAX + 1;
103 lwkt_gettoken_shared(&p->p_token);
104 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred))
106 lwkt_reltoken(&p->p_token);
113 lwkt_gettoken_shared(&curp->p_token);
116 lwkt_reltoken(&curp->p_token);
117 } else if ((pg = pgfind(who)) == NULL) {
119 } /* else ref held from pgfind */
121 lwkt_gettoken_shared(&pg->pg_token);
122 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
123 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred) &&
128 lwkt_reltoken(&pg->pg_token);
133 who = curtd->td_ucred->cr_uid;
136 allproc_scan(getpriority_callback, &info, 0);
144 if (low == PRIO_MAX + 1) {
148 sysmsg->sysmsg_result = low;
155 * Figure out the current lowest nice priority for processes owned
156 * by the specified user.
160 getpriority_callback(struct proc *p, void *data)
162 struct getpriority_info *info = data;
164 lwkt_gettoken_shared(&p->p_token);
165 if (PRISON_CHECK(curthread->td_ucred, p->p_ucred) &&
166 p->p_ucred->cr_uid == info->who &&
167 p->p_nice < info->low) {
168 info->low = p->p_nice;
170 lwkt_reltoken(&p->p_token);
174 struct setpriority_info {
181 static int setpriority_callback(struct proc *p, void *data);
187 sys_setpriority(struct sysmsg *sysmsg, const struct setpriority_args *uap)
189 struct setpriority_info info;
190 thread_t curtd = curthread;
191 struct proc *curp = curproc;
194 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(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 who = curtd->td_ucred->cr_uid;
250 info.prio = uap->prio;
254 allproc_scan(setpriority_callback, &info, 0);
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 sysmsg *sysmsg, const 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;
339 switch (uap->which) {
342 high = curp->p_ionice;
346 lwkt_gettoken_shared(&p->p_token);
347 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred))
349 lwkt_reltoken(&p->p_token);
356 lwkt_gettoken_shared(&curp->p_token);
359 lwkt_reltoken(&curp->p_token);
360 } else if ((pg = pgfind(who)) == NULL) {
362 } /* else ref held from pgfind */
364 lwkt_gettoken_shared(&pg->pg_token);
365 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
366 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred) &&
370 lwkt_reltoken(&pg->pg_token);
375 who = curtd->td_ucred->cr_uid;
378 allproc_scan(ioprio_get_callback, &info, 0);
385 if (high == IOPRIO_MIN-2) {
389 sysmsg->sysmsg_result = high;
396 * Figure out the current lowest nice priority for processes owned
397 * by the specified user.
401 ioprio_get_callback(struct proc *p, void *data)
403 struct ioprio_get_info *info = data;
405 lwkt_gettoken_shared(&p->p_token);
406 if (PRISON_CHECK(curthread->td_ucred, p->p_ucred) &&
407 p->p_ucred->cr_uid == info->who &&
408 p->p_ionice > info->high) {
409 info->high = p->p_ionice;
411 lwkt_reltoken(&p->p_token);
416 struct ioprio_set_info {
423 static int ioprio_set_callback(struct proc *p, void *data);
429 sys_ioprio_set(struct sysmsg *sysmsg, const struct ioprio_set_args *uap)
431 struct ioprio_set_info info;
432 thread_t curtd = curthread;
433 struct proc *curp = curproc;
436 int found = 0, error = 0;
439 switch (uap->which) {
442 lwkt_gettoken(&curp->p_token);
443 error = doionice(curp, uap->prio);
444 lwkt_reltoken(&curp->p_token);
449 lwkt_gettoken(&p->p_token);
450 if (PRISON_CHECK(curtd->td_ucred, p->p_ucred)) {
451 error = doionice(p, uap->prio);
454 lwkt_reltoken(&p->p_token);
461 lwkt_gettoken_shared(&curp->p_token);
464 lwkt_reltoken(&curp->p_token);
465 } else if ((pg = pgfind(who)) == NULL) {
467 } /* else ref held from pgfind */
469 lwkt_gettoken(&pg->pg_token);
471 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
473 lwkt_gettoken(&p->p_token);
474 if (p->p_pgrp == pg &&
475 PRISON_CHECK(curtd->td_ucred, p->p_ucred)) {
476 error = doionice(p, uap->prio);
479 lwkt_reltoken(&p->p_token);
480 if (p->p_pgrp != pg) {
486 lwkt_reltoken(&pg->pg_token);
491 who = curtd->td_ucred->cr_uid;
492 info.prio = uap->prio;
496 allproc_scan(ioprio_set_callback, &info, 0);
513 ioprio_set_callback(struct proc *p, void *data)
515 struct ioprio_set_info *info = data;
518 lwkt_gettoken(&p->p_token);
519 if (p->p_ucred->cr_uid == info->who &&
520 PRISON_CHECK(curthread->td_ucred, p->p_ucred)) {
521 error = doionice(p, info->prio);
526 lwkt_reltoken(&p->p_token);
531 doionice(struct proc *chgp, int n)
533 struct ucred *cr = curthread->td_ucred;
535 if (cr->cr_uid && cr->cr_ruid &&
536 cr->cr_uid != chgp->p_ucred->cr_uid &&
537 cr->cr_ruid != chgp->p_ucred->cr_uid)
543 if (n < chgp->p_ionice &&
544 priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
556 sys_lwp_rtprio(struct sysmsg *sysmsg, const struct lwp_rtprio_args *uap)
558 struct ucred *cr = curthread->td_ucred;
564 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
580 lwkt_gettoken(&p->p_token);
586 if (uap->tid == -1) {
588 * sadly, tid can be 0 so we can't use 0 here
591 lp = curthread->td_lwp;
593 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
601 * Make sure that this lwp is not ripped if any of the following
602 * code blocks, e.g. copyout.
605 switch (uap->function) {
607 error = copyout(&lp->lwp_rtprio, uap->rtp,
608 sizeof(struct rtprio));
611 if (cr->cr_uid && cr->cr_ruid &&
612 cr->cr_uid != p->p_ucred->cr_uid &&
613 cr->cr_ruid != p->p_ucred->cr_uid) {
617 /* disallow setting rtprio in most cases if not superuser */
618 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
619 /* can't set someone else's */
620 if (uap->pid) { /* XXX */
624 /* can't set realtime priority */
626 * Realtime priority has to be restricted for reasons which should be
627 * obvious. However, for idle priority, there is a potential for
628 * system deadlock if an idleprio process gains a lock on a resource
629 * that other processes need (and the idleprio process can't run
630 * due to a CPU-bound normal process). Fix me! XXX
632 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
641 case RTP_PRIO_REALTIME:
642 case RTP_PRIO_NORMAL:
644 if (rtp.prio > RTP_PRIO_MAX) {
647 lp->lwp_rtprio = rtp;
664 lwkt_reltoken(&p->p_token);
671 * Set realtime priority
676 sys_rtprio(struct sysmsg *sysmsg, const struct rtprio_args *uap)
678 struct ucred *cr = curthread->td_ucred;
684 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
699 lwkt_gettoken(&p->p_token);
702 lp = FIRST_LWP_IN_PROC(p);
703 switch (uap->function) {
705 error = copyout(&lp->lwp_rtprio, uap->rtp,
706 sizeof(struct rtprio));
709 if (cr->cr_uid && cr->cr_ruid &&
710 cr->cr_uid != p->p_ucred->cr_uid &&
711 cr->cr_ruid != p->p_ucred->cr_uid) {
715 /* disallow setting rtprio in most cases if not superuser */
716 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
717 /* can't set someone else's */
722 /* can't set realtime priority */
724 * Realtime priority has to be restricted for reasons which should be
725 * obvious. However, for idle priority, there is a potential for
726 * system deadlock if an idleprio process gains a lock on a resource
727 * that other processes need (and the idleprio process can't run
728 * due to a CPU-bound normal process). Fix me! XXX
730 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
739 case RTP_PRIO_REALTIME:
740 case RTP_PRIO_NORMAL:
742 if (rtp.prio > RTP_PRIO_MAX) {
746 lp->lwp_rtprio = rtp;
760 lwkt_reltoken(&p->p_token);
768 * Transform the running time and tick information in lwp lp's thread into user,
769 * system, and interrupt time usage.
771 * Since we are limited to statclock tick granularity this is a statisical
772 * calculation which will be correct over the long haul, but should not be
773 * expected to measure fine grained deltas.
775 * It is possible to catch a lwp in the midst of being created, so
776 * check whether lwp_thread is NULL or not.
779 calcru(struct lwp *lp, struct timeval *up, struct timeval *sp)
784 * Calculate at the statclock level. YYY if the thread is owned by
785 * another cpu we need to forward the request to the other cpu, or
786 * have a token to interlock the information in order to avoid racing
787 * thread destruction.
789 if ((td = lp->lwp_thread) != NULL) {
791 up->tv_sec = td->td_uticks / 1000000;
792 up->tv_usec = td->td_uticks % 1000000;
793 sp->tv_sec = td->td_sticks / 1000000;
794 sp->tv_usec = td->td_sticks % 1000000;
800 * Aggregate resource statistics of all lwps of a process.
802 * proc.p_ru keeps track of all statistics directly related to a proc. This
803 * consists of RSS usage and nswap information and aggregate numbers for all
804 * former lwps of this proc.
806 * proc.p_cru is the sum of all stats of reaped children.
808 * lwp.lwp_ru contains the stats directly related to one specific lwp, meaning
809 * packet, scheduler switch or page fault counts, etc. This information gets
810 * added to lwp.lwp_proc.p_ru when the lwp exits.
813 calcru_proc(struct proc *p, struct rusage *ru)
815 struct timeval upt, spt;
821 FOREACH_LWP_IN_PROC(lp, p) {
822 calcru(lp, &upt, &spt);
823 timevaladd(&ru->ru_utime, &upt);
824 timevaladd(&ru->ru_stime, &spt);
825 for (rip1 = &ru->ru_first, rip2 = &lp->lwp_ru.ru_first;
826 rip1 <= &ru->ru_last;
837 sys_getrusage(struct sysmsg *sysmsg, const struct getrusage_args *uap)
839 struct proc *p = curproc;
844 lwkt_gettoken(&p->p_token);
852 case RUSAGE_CHILDREN:
860 lwkt_reltoken(&p->p_token);
863 error = copyout(rup, uap->rusage, sizeof(struct rusage));
868 ruadd(struct rusage *ru, struct rusage *ru2)
873 timevaladd(&ru->ru_utime, &ru2->ru_utime);
874 timevaladd(&ru->ru_stime, &ru2->ru_stime);
875 if (ru->ru_maxrss < ru2->ru_maxrss)
876 ru->ru_maxrss = ru2->ru_maxrss;
877 ip = &ru->ru_first; ip2 = &ru2->ru_first;
878 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
883 * Find the uidinfo structure for a uid. This structure is used to
884 * track the total resource consumption (process count, socket buffer
885 * size, etc.) for the uid and impose limits.
890 spin_init(&uihash_lock, "uihashinit");
891 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
895 * NOTE: Must be called with uihash_lock held
897 static struct uidinfo *
900 struct uihashhead *uipp;
904 LIST_FOREACH(uip, uipp, ui_hash) {
905 if (uip->ui_uid == uid)
912 * Helper function to creat ea uid that could not be found.
913 * This function will properly deal with races.
915 * WARNING! Should only be used by this source file and by the proc0
921 struct uidinfo *uip, *tmp;
924 * Allocate space and check for a race
926 uip = kmalloc(sizeof(*uip), M_UIDINFO, M_WAITOK | M_ZERO);
929 * Initialize structure and enter it into the hash table
931 spin_init(&uip->ui_lock, "uicreate");
933 uip->ui_ref = 1; /* we're returning a ref */
934 varsymset_init(&uip->ui_varsymset, NULL);
935 uip->ui_pcpu = kmalloc(sizeof(*uip->ui_pcpu) * ncpus,
936 M_UIDINFO, M_WAITOK | M_ZERO);
939 * Somebody may have already created the uidinfo for this
940 * uid. If so, return that instead.
942 spin_lock(&uihash_lock);
946 spin_unlock(&uihash_lock);
948 spin_uninit(&uip->ui_lock);
949 varsymset_clean(&uip->ui_varsymset);
950 kfree(uip->ui_pcpu, M_UIDINFO);
951 kfree(uip, M_UIDINFO);
954 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
955 spin_unlock(&uihash_lock);
961 * Find the uidinfo for a uid, creating one if necessary
967 thread_t td = curthread;
970 uip = td->td_ucred->cr_uidinfo;
971 if (uip->ui_uid == uid) {
975 uip = td->td_ucred->cr_ruidinfo;
976 if (uip->ui_uid == uid) {
982 spin_lock_shared(&uihash_lock);
985 spin_unlock_shared(&uihash_lock);
989 spin_unlock_shared(&uihash_lock);
995 * Helper funtion to remove a uidinfo whos reference count may
996 * have transitioned to 0. The reference count is likely 0
1002 struct uidinfo *uip;
1005 * If we are still the only holder after acquiring the uihash_lock
1006 * we can safely unlink the uip and destroy it. Otherwise we lost
1007 * a race and must fail.
1009 spin_lock(&uihash_lock);
1010 uip = uilookup(uid);
1011 if (uip && uip->ui_ref == 0) {
1012 LIST_REMOVE(uip, ui_hash);
1013 spin_unlock(&uihash_lock);
1016 * The uip is now orphaned and we can destroy it at our
1019 if (uip->ui_sbsize != 0)
1020 kprintf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1021 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1022 if (uip->ui_proccnt != 0)
1023 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1024 uip->ui_uid, uip->ui_proccnt);
1026 varsymset_clean(&uip->ui_varsymset);
1027 lockuninit(&uip->ui_varsymset.vx_lock);
1028 spin_uninit(&uip->ui_lock);
1029 kfree(uip->ui_pcpu, M_UIDINFO);
1030 kfree(uip, M_UIDINFO);
1032 spin_unlock(&uihash_lock);
1037 * Bump the ref count
1040 uihold(struct uidinfo *uip)
1042 KKASSERT(uip->ui_ref >= 0);
1043 atomic_add_int(&uip->ui_ref, 1);
1047 * Drop the ref count. The last-drop code still needs to remove the
1048 * uidinfo from the hash table which it does by re-looking-it-up.
1050 * NOTE: The uip can be ripped out from under us after the fetchadd.
1053 uidrop(struct uidinfo *uip)
1057 KKASSERT(uip->ui_ref > 0);
1060 if (atomic_fetchadd_int(&uip->ui_ref, -1) == 1) {
1066 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
1073 * Change the count associated with number of processes
1074 * a given user is using.
1076 * NOTE: When 'max' is 0, don't enforce a limit.
1078 * NOTE: Due to concurrency, the count can sometimes exceed the max
1079 * by a small amount.
1082 chgproccnt(struct uidinfo *uip, int diff, int max)
1086 /* don't allow them to exceed max, but allow subtraction */
1087 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1090 atomic_add_long(&uip->ui_proccnt, diff);
1091 if (uip->ui_proccnt < 0)
1092 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
1099 * Change the total socket buffer size a user has used.
1102 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
1108 sbsize = atomic_fetchadd_long(&uip->ui_sbsize, to - *hiwat);
1109 new = sbsize + to - *hiwat;
1113 * If we are trying to increase the socket buffer size
1114 * Scale down the hi water mark when we exceed the user's
1115 * allowed socket buffer space.
1117 * We can't scale down too much or we will blow up atomic packet
1120 if (to > *hiwat && to > MCLBYTES && new > max) {
1121 to = to * max / new;