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 "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
44 #include <sys/kern_syscall.h>
45 #include <sys/kernel.h>
46 #include <sys/resourcevar.h>
47 #include <sys/malloc.h>
51 #include <sys/lockf.h>
54 #include <vm/vm_param.h>
57 #include <vm/vm_map.h>
59 #include <sys/thread2.h>
60 #include <sys/spinlock2.h>
62 static int donice (struct proc *chgp, int n);
63 static int doionice (struct proc *chgp, int n);
65 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
66 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
67 static struct spinlock uihash_lock;
68 static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
69 static u_long uihash; /* size of hash table - 1 */
71 static struct uidinfo *uicreate (uid_t uid);
72 static struct uidinfo *uilookup (uid_t uid);
75 * Resource controls and accounting.
78 struct getpriority_info {
83 static int getpriority_callback(struct proc *p, void *data);
89 sys_getpriority(struct getpriority_args *uap)
91 struct getpriority_info info;
92 struct proc *curp = curproc;
94 int low = PRIO_MAX + 1;
106 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
120 } else if ((pg = pgfind(uap->who)) == NULL) {
122 } /* else ref held from pgfind */
124 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
125 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
135 uap->who = curp->p_ucred->cr_uid;
138 allproc_scan(getpriority_callback, &info);
146 if (low == PRIO_MAX + 1) {
150 uap->sysmsg_result = low;
157 * Figure out the current lowest nice priority for processes owned
158 * by the specified user.
162 getpriority_callback(struct proc *p, void *data)
164 struct getpriority_info *info = data;
166 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
167 p->p_ucred->cr_uid == info->who &&
168 p->p_nice < info->low) {
169 info->low = p->p_nice;
174 struct setpriority_info {
181 static int setpriority_callback(struct proc *p, void *data);
187 sys_setpriority(struct setpriority_args *uap)
189 struct setpriority_info info;
190 struct proc *curp = curproc;
192 int found = 0, error = 0;
194 lwkt_gettoken(&proc_token);
196 switch (uap->which) {
205 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
206 error = donice(p, uap->prio);
220 } else if ((pg = pgfind(uap->who)) == NULL) {
222 } /* else ref held from pgfind */
224 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
225 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
226 error = donice(p, uap->prio);
235 uap->who = curp->p_ucred->cr_uid;
236 info.prio = uap->prio;
240 allproc_scan(setpriority_callback, &info);
251 lwkt_reltoken(&proc_token);
260 setpriority_callback(struct proc *p, void *data)
262 struct setpriority_info *info = data;
265 if (p->p_ucred->cr_uid == info->who &&
266 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
267 error = donice(p, info->prio);
276 donice(struct proc *chgp, int n)
278 struct proc *curp = curproc;
279 struct ucred *cr = curp->p_ucred;
282 if (cr->cr_uid && cr->cr_ruid &&
283 cr->cr_uid != chgp->p_ucred->cr_uid &&
284 cr->cr_ruid != chgp->p_ucred->cr_uid)
290 if (n < chgp->p_nice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
293 FOREACH_LWP_IN_PROC(lp, chgp) {
295 chgp->p_usched->resetpriority(lp);
302 struct ioprio_get_info {
307 static int ioprio_get_callback(struct proc *p, void *data);
313 sys_ioprio_get(struct ioprio_get_args *uap)
315 struct ioprio_get_info info;
316 struct proc *curp = curproc;
318 int high = IOPRIO_MIN-2;
321 lwkt_gettoken(&proc_token);
323 switch (uap->which) {
332 if (PRISON_CHECK(curp->p_ucred, p->p_ucred))
345 } else if ((pg = pgfind(uap->who)) == NULL) {
347 } /* else ref held from pgfind */
349 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
350 if (PRISON_CHECK(curp->p_ucred, p->p_ucred) &&
359 uap->who = curp->p_ucred->cr_uid;
362 allproc_scan(ioprio_get_callback, &info);
370 if (high == IOPRIO_MIN-2) {
374 uap->sysmsg_result = high;
377 lwkt_reltoken(&proc_token);
383 * Figure out the current lowest nice priority for processes owned
384 * by the specified user.
388 ioprio_get_callback(struct proc *p, void *data)
390 struct ioprio_get_info *info = data;
392 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
393 p->p_ucred->cr_uid == info->who &&
394 p->p_ionice > info->high) {
395 info->high = p->p_ionice;
401 struct ioprio_set_info {
408 static int ioprio_set_callback(struct proc *p, void *data);
414 sys_ioprio_set(struct ioprio_set_args *uap)
416 struct ioprio_set_info info;
417 struct proc *curp = curproc;
419 int found = 0, error = 0;
421 lwkt_gettoken(&proc_token);
423 switch (uap->which) {
432 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
433 error = doionice(p, uap->prio);
447 } else if ((pg = pgfind(uap->who)) == NULL) {
449 } /* else ref held from pgfind */
451 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
452 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
453 error = doionice(p, uap->prio);
462 uap->who = curp->p_ucred->cr_uid;
463 info.prio = uap->prio;
467 allproc_scan(ioprio_set_callback, &info);
478 lwkt_reltoken(&proc_token);
487 ioprio_set_callback(struct proc *p, void *data)
489 struct ioprio_set_info *info = data;
492 if (p->p_ucred->cr_uid == info->who &&
493 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
494 error = doionice(p, info->prio);
503 doionice(struct proc *chgp, int n)
505 struct proc *curp = curproc;
506 struct ucred *cr = curp->p_ucred;
508 if (cr->cr_uid && cr->cr_ruid &&
509 cr->cr_uid != chgp->p_ucred->cr_uid &&
510 cr->cr_ruid != chgp->p_ucred->cr_uid)
516 if (n < chgp->p_ionice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
528 sys_lwp_rtprio(struct lwp_rtprio_args *uap)
533 struct ucred *cr = curthread->td_ucred;
536 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
542 lwkt_gettoken(&proc_token);
560 if (uap->tid == -1) {
562 * sadly, tid can be 0 so we can't use 0 here
565 lp = curthread->td_lwp;
567 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
574 switch (uap->function) {
576 error = copyout(&lp->lwp_rtprio, uap->rtp,
577 sizeof(struct rtprio));
580 if (cr->cr_uid && cr->cr_ruid &&
581 cr->cr_uid != p->p_ucred->cr_uid &&
582 cr->cr_ruid != p->p_ucred->cr_uid) {
586 /* disallow setting rtprio in most cases if not superuser */
587 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
588 /* can't set someone else's */
589 if (uap->pid) { /* XXX */
593 /* can't set realtime priority */
595 * Realtime priority has to be restricted for reasons which should be
596 * obvious. However, for idle priority, there is a potential for
597 * system deadlock if an idleprio process gains a lock on a resource
598 * that other processes need (and the idleprio process can't run
599 * due to a CPU-bound normal process). Fix me! XXX
601 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
610 case RTP_PRIO_REALTIME:
611 case RTP_PRIO_NORMAL:
613 if (rtp.prio > RTP_PRIO_MAX) {
616 lp->lwp_rtprio = rtp;
633 lwkt_reltoken(&proc_token);
639 * Set realtime priority
644 sys_rtprio(struct rtprio_args *uap)
648 struct ucred *cr = curthread->td_ucred;
652 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
656 lwkt_gettoken(&proc_token);
671 lp = FIRST_LWP_IN_PROC(p);
672 switch (uap->function) {
674 error = copyout(&lp->lwp_rtprio, uap->rtp,
675 sizeof(struct rtprio));
678 if (cr->cr_uid && cr->cr_ruid &&
679 cr->cr_uid != p->p_ucred->cr_uid &&
680 cr->cr_ruid != p->p_ucred->cr_uid) {
684 /* disallow setting rtprio in most cases if not superuser */
685 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
686 /* can't set someone else's */
691 /* can't set realtime priority */
693 * Realtime priority has to be restricted for reasons which should be
694 * obvious. However, for idle priority, there is a potential for
695 * system deadlock if an idleprio process gains a lock on a resource
696 * that other processes need (and the idleprio process can't run
697 * due to a CPU-bound normal process). Fix me! XXX
699 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
708 case RTP_PRIO_REALTIME:
709 case RTP_PRIO_NORMAL:
711 if (rtp.prio > RTP_PRIO_MAX) {
715 lp->lwp_rtprio = rtp;
730 lwkt_reltoken(&proc_token);
739 sys_setrlimit(struct __setrlimit_args *uap)
744 error = copyin(uap->rlp, &alim, sizeof(alim));
748 error = kern_setrlimit(uap->which, &alim);
757 sys_getrlimit(struct __getrlimit_args *uap)
762 error = kern_getrlimit(uap->which, &lim);
765 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
770 * Transform the running time and tick information in lwp lp's thread into user,
771 * system, and interrupt time usage.
773 * Since we are limited to statclock tick granularity this is a statisical
774 * calculation which will be correct over the long haul, but should not be
775 * expected to measure fine grained deltas.
777 * It is possible to catch a lwp in the midst of being created, so
778 * check whether lwp_thread is NULL or not.
781 calcru(struct lwp *lp, struct timeval *up, struct timeval *sp)
786 * Calculate at the statclock level. YYY if the thread is owned by
787 * another cpu we need to forward the request to the other cpu, or
788 * have a token to interlock the information in order to avoid racing
789 * thread destruction.
791 if ((td = lp->lwp_thread) != NULL) {
793 up->tv_sec = td->td_uticks / 1000000;
794 up->tv_usec = td->td_uticks % 1000000;
795 sp->tv_sec = td->td_sticks / 1000000;
796 sp->tv_usec = td->td_sticks % 1000000;
802 * Aggregate resource statistics of all lwps of a process.
804 * proc.p_ru keeps track of all statistics directly related to a proc. This
805 * consists of RSS usage and nswap information and aggregate numbers for all
806 * former lwps of this proc.
808 * proc.p_cru is the sum of all stats of reaped children.
810 * lwp.lwp_ru contains the stats directly related to one specific lwp, meaning
811 * packet, scheduler switch or page fault counts, etc. This information gets
812 * added to lwp.lwp_proc.p_ru when the lwp exits.
815 calcru_proc(struct proc *p, struct rusage *ru)
817 struct timeval upt, spt;
823 FOREACH_LWP_IN_PROC(lp, p) {
824 calcru(lp, &upt, &spt);
825 timevaladd(&ru->ru_utime, &upt);
826 timevaladd(&ru->ru_stime, &spt);
827 for (rip1 = &ru->ru_first, rip2 = &lp->lwp_ru.ru_first;
828 rip1 <= &ru->ru_last;
839 sys_getrusage(struct getrusage_args *uap)
845 lwkt_gettoken(&proc_token);
850 calcru_proc(curproc, rup);
853 case RUSAGE_CHILDREN:
854 rup = &curproc->p_cru;
862 error = copyout(rup, uap->rusage, sizeof(struct rusage));
863 lwkt_reltoken(&proc_token);
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);
891 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
895 * NOTE: Must be called with uihash_lock held
899 static struct uidinfo *
902 struct uihashhead *uipp;
906 LIST_FOREACH(uip, uipp, ui_hash) {
907 if (uip->ui_uid == uid)
914 * Helper function to creat ea uid that could not be found.
915 * This function will properly deal with races.
919 static struct uidinfo *
922 struct uidinfo *uip, *tmp;
925 * Allocate space and check for a race
927 uip = kmalloc(sizeof(*uip), M_UIDINFO, M_WAITOK|M_ZERO);
930 * Initialize structure and enter it into the hash table
932 spin_init(&uip->ui_lock);
934 uip->ui_ref = 1; /* we're returning a ref */
935 varsymset_init(&uip->ui_varsymset, NULL);
938 * Somebody may have already created the uidinfo for this
939 * uid. If so, return that instead.
941 spin_lock(&uihash_lock);
945 spin_unlock(&uihash_lock);
947 spin_uninit(&uip->ui_lock);
948 varsymset_clean(&uip->ui_varsymset);
949 kfree(uip, M_UIDINFO);
952 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
953 spin_unlock(&uihash_lock);
968 spin_lock(&uihash_lock);
971 spin_unlock(&uihash_lock);
975 spin_unlock(&uihash_lock);
981 * Helper funtion to remove a uidinfo whos reference count is
982 * transitioning from 1->0. The reference count is 1 on call.
984 * Zero is returned on success, otherwise non-zero and the
985 * uiphas not been removed.
990 uifree(struct uidinfo *uip)
993 * If we are still the only holder after acquiring the uihash_lock
994 * we can safely unlink the uip and destroy it. Otherwise we lost
995 * a race and must fail.
997 spin_lock(&uihash_lock);
998 if (uip->ui_ref != 1) {
999 spin_unlock(&uihash_lock);
1002 LIST_REMOVE(uip, ui_hash);
1003 spin_unlock(&uihash_lock);
1006 * The uip is now orphaned and we can destroy it at our
1009 if (uip->ui_sbsize != 0)
1010 kprintf("freeing uidinfo: uid = %d, sbsize = %jd\n",
1011 uip->ui_uid, (intmax_t)uip->ui_sbsize);
1012 if (uip->ui_proccnt != 0)
1013 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
1014 uip->ui_uid, uip->ui_proccnt);
1016 varsymset_clean(&uip->ui_varsymset);
1017 lockuninit(&uip->ui_varsymset.vx_lock);
1018 spin_uninit(&uip->ui_lock);
1019 kfree(uip, M_UIDINFO);
1027 uihold(struct uidinfo *uip)
1029 atomic_add_int(&uip->ui_ref, 1);
1030 KKASSERT(uip->ui_ref >= 0);
1034 * NOTE: It is important for us to not drop the ref count to 0
1035 * because this can cause a 2->0/2->0 race with another
1036 * concurrent dropper. Losing the race in that situation
1037 * can cause uip to become stale for one of the other
1043 uidrop(struct uidinfo *uip)
1047 KKASSERT(uip->ui_ref > 0);
1053 if (uifree(uip) == 0)
1055 } else if (atomic_cmpset_int(&uip->ui_ref, ref, ref - 1)) {
1063 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
1070 * Change the count associated with number of processes
1071 * a given user is using. When 'max' is 0, don't enforce a limit
1074 chgproccnt(struct uidinfo *uip, int diff, int max)
1077 spin_lock(&uip->ui_lock);
1078 /* don't allow them to exceed max, but allow subtraction */
1079 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
1082 uip->ui_proccnt += diff;
1083 if (uip->ui_proccnt < 0)
1084 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
1087 spin_unlock(&uip->ui_lock);
1092 * Change the total socket buffer size a user has used.
1095 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
1099 spin_lock(&uip->ui_lock);
1100 new = uip->ui_sbsize + to - *hiwat;
1104 * If we are trying to increase the socket buffer size
1105 * Scale down the hi water mark when we exceed the user's
1106 * allowed socket buffer space.
1108 * We can't scale down too much or we will blow up atomic packet
1111 if (to > *hiwat && to > MCLBYTES && new > max) {
1112 to = to * max / new;
1116 uip->ui_sbsize = new;
1118 spin_unlock(&uip->ui_lock);