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 $
40 * $DragonFly: src/sys/kern/kern_resource.c,v 1.35 2008/05/27 05:25:34 dillon Exp $
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
49 #include <sys/kern_syscall.h>
50 #include <sys/kernel.h>
51 #include <sys/resourcevar.h>
52 #include <sys/malloc.h>
56 #include <sys/lockf.h>
59 #include <vm/vm_param.h>
62 #include <vm/vm_map.h>
64 #include <sys/thread2.h>
65 #include <sys/spinlock2.h>
67 static int donice (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;
103 switch (uap->which) {
111 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
122 else if ((pg = pgfind(uap->who)) == NULL)
124 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
125 if ((PRISON_CHECK(curp->p_ucred, p->p_ucred) && p->p_nice < low))
132 uap->who = curp->p_ucred->cr_uid;
135 allproc_scan(getpriority_callback, &info);
143 if (low == PRIO_MAX + 1) {
147 uap->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 if (PRISON_CHECK(curproc->p_ucred, p->p_ucred) &&
165 p->p_ucred->cr_uid == info->who &&
166 p->p_nice < info->low) {
167 info->low = p->p_nice;
172 struct setpriority_info {
179 static int setpriority_callback(struct proc *p, void *data);
185 sys_setpriority(struct setpriority_args *uap)
187 struct setpriority_info info;
188 struct proc *curp = curproc;
190 int found = 0, error = 0;
194 switch (uap->which) {
202 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
204 error = donice(p, uap->prio);
214 else if ((pg = pgfind(uap->who)) == NULL)
216 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
217 if (PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
218 error = donice(p, uap->prio);
226 uap->who = curp->p_ucred->cr_uid;
227 info.prio = uap->prio;
231 allproc_scan(setpriority_callback, &info);
250 setpriority_callback(struct proc *p, void *data)
252 struct setpriority_info *info = data;
255 if (p->p_ucred->cr_uid == info->who &&
256 PRISON_CHECK(curproc->p_ucred, p->p_ucred)) {
257 error = donice(p, info->prio);
266 donice(struct proc *chgp, int n)
268 struct proc *curp = curproc;
269 struct ucred *cr = curp->p_ucred;
272 if (cr->cr_uid && cr->cr_ruid &&
273 cr->cr_uid != chgp->p_ucred->cr_uid &&
274 cr->cr_ruid != chgp->p_ucred->cr_uid)
280 if (n < chgp->p_nice && priv_check_cred(cr, PRIV_SCHED_SETPRIORITY, 0))
283 FOREACH_LWP_IN_PROC(lp, chgp)
284 chgp->p_usched->resetpriority(lp);
292 sys_lwp_rtprio(struct lwp_rtprio_args *uap)
294 struct proc *p = curproc;
297 struct ucred *cr = curthread->td_ucred;
300 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
308 /* curproc already loaded on p */
322 if (uap->tid == -1) {
324 * sadly, tid can be 0 so we can't use 0 here
327 lp = curthread->td_lwp;
329 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
336 switch (uap->function) {
338 error = copyout(&lp->lwp_rtprio, uap->rtp,
339 sizeof(struct rtprio));
342 if (cr->cr_uid && cr->cr_ruid &&
343 cr->cr_uid != p->p_ucred->cr_uid &&
344 cr->cr_ruid != p->p_ucred->cr_uid) {
348 /* disallow setting rtprio in most cases if not superuser */
349 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
350 /* can't set someone else's */
351 if (uap->pid) { /* XXX */
355 /* can't set realtime priority */
357 * Realtime priority has to be restricted for reasons which should be
358 * obvious. However, for idle priority, there is a potential for
359 * system deadlock if an idleprio process gains a lock on a resource
360 * that other processes need (and the idleprio process can't run
361 * due to a CPU-bound normal process). Fix me! XXX
363 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
372 case RTP_PRIO_REALTIME:
373 case RTP_PRIO_NORMAL:
375 if (rtp.prio > RTP_PRIO_MAX)
377 lp->lwp_rtprio = rtp;
396 * Set realtime priority
401 sys_rtprio(struct rtprio_args *uap)
403 struct proc *curp = curproc;
406 struct ucred *cr = curthread->td_ucred;
410 error = copyin(uap->rtp, &rtp, sizeof(struct rtprio));
426 lp = FIRST_LWP_IN_PROC(p);
427 switch (uap->function) {
429 error = copyout(&lp->lwp_rtprio, uap->rtp,
430 sizeof(struct rtprio));
433 if (cr->cr_uid && cr->cr_ruid &&
434 cr->cr_uid != p->p_ucred->cr_uid &&
435 cr->cr_ruid != p->p_ucred->cr_uid) {
439 /* disallow setting rtprio in most cases if not superuser */
440 if (priv_check_cred(cr, PRIV_SCHED_RTPRIO, 0)) {
441 /* can't set someone else's */
446 /* can't set realtime priority */
448 * Realtime priority has to be restricted for reasons which should be
449 * obvious. However, for idle priority, there is a potential for
450 * system deadlock if an idleprio process gains a lock on a resource
451 * that other processes need (and the idleprio process can't run
452 * due to a CPU-bound normal process). Fix me! XXX
454 if (RTP_PRIO_IS_REALTIME(rtp.type)) {
463 case RTP_PRIO_REALTIME:
464 case RTP_PRIO_NORMAL:
466 if (rtp.prio > RTP_PRIO_MAX) {
470 lp->lwp_rtprio = rtp;
491 sys_setrlimit(struct __setrlimit_args *uap)
496 error = copyin(uap->rlp, &alim, sizeof(alim));
500 error = kern_setrlimit(uap->which, &alim);
509 sys_getrlimit(struct __getrlimit_args *uap)
514 error = kern_getrlimit(uap->which, &lim);
517 error = copyout(&lim, uap->rlp, sizeof(*uap->rlp));
522 * Transform the running time and tick information in lwp lp's thread into user,
523 * system, and interrupt time usage.
525 * Since we are limited to statclock tick granularity this is a statisical
526 * calculation which will be correct over the long haul, but should not be
527 * expected to measure fine grained deltas.
529 * It is possible to catch a lwp in the midst of being created, so
530 * check whether lwp_thread is NULL or not.
533 calcru(struct lwp *lp, struct timeval *up, struct timeval *sp)
538 * Calculate at the statclock level. YYY if the thread is owned by
539 * another cpu we need to forward the request to the other cpu, or
540 * have a token to interlock the information in order to avoid racing
541 * thread destruction.
543 if ((td = lp->lwp_thread) != NULL) {
545 up->tv_sec = td->td_uticks / 1000000;
546 up->tv_usec = td->td_uticks % 1000000;
547 sp->tv_sec = td->td_sticks / 1000000;
548 sp->tv_usec = td->td_sticks % 1000000;
554 * Aggregate resource statistics of all lwps of a process.
556 * proc.p_ru keeps track of all statistics directly related to a proc. This
557 * consists of RSS usage and nswap information and aggregate numbers for all
558 * former lwps of this proc.
560 * proc.p_cru is the sum of all stats of reaped children.
562 * lwp.lwp_ru contains the stats directly related to one specific lwp, meaning
563 * packet, scheduler switch or page fault counts, etc. This information gets
564 * added to lwp.lwp_proc.p_ru when the lwp exits.
567 calcru_proc(struct proc *p, struct rusage *ru)
569 struct timeval upt, spt;
575 FOREACH_LWP_IN_PROC(lp, p) {
576 calcru(lp, &upt, &spt);
577 timevaladd(&ru->ru_utime, &upt);
578 timevaladd(&ru->ru_stime, &spt);
579 for (rip1 = &ru->ru_first, rip2 = &lp->lwp_ru.ru_first;
580 rip1 <= &ru->ru_last;
591 sys_getrusage(struct getrusage_args *uap)
602 calcru_proc(curproc, rup);
605 case RUSAGE_CHILDREN:
606 rup = &curproc->p_cru;
614 error = copyout(rup, uap->rusage, sizeof(struct rusage));
620 ruadd(struct rusage *ru, struct rusage *ru2)
625 timevaladd(&ru->ru_utime, &ru2->ru_utime);
626 timevaladd(&ru->ru_stime, &ru2->ru_stime);
627 if (ru->ru_maxrss < ru2->ru_maxrss)
628 ru->ru_maxrss = ru2->ru_maxrss;
629 ip = &ru->ru_first; ip2 = &ru2->ru_first;
630 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
635 * Find the uidinfo structure for a uid. This structure is used to
636 * track the total resource consumption (process count, socket buffer
637 * size, etc.) for the uid and impose limits.
642 spin_init(&uihash_lock);
643 uihashtbl = hashinit(maxproc / 16, M_UIDINFO, &uihash);
647 * NOTE: Must be called with uihash_lock held
651 static struct uidinfo *
654 struct uihashhead *uipp;
658 LIST_FOREACH(uip, uipp, ui_hash) {
659 if (uip->ui_uid == uid)
668 static struct uidinfo *
671 struct uidinfo *uip, *tmp;
673 * Allocate space and check for a race
675 MALLOC(uip, struct uidinfo *, sizeof(*uip), M_UIDINFO, M_WAITOK);
677 * Initialize structure and enter it into the hash table
679 spin_init(&uip->ui_lock);
683 uip->ui_ref = 1; /* we're returning a ref */
684 uip->ui_posixlocks = 0;
685 varsymset_init(&uip->ui_varsymset, NULL);
688 * Somebody may have already created the uidinfo for this
689 * uid. If so, return that instead.
691 spin_lock_wr(&uihash_lock);
694 varsymset_clean(&uip->ui_varsymset);
695 spin_uninit(&uip->ui_lock);
696 FREE(uip, M_UIDINFO);
699 LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
701 spin_unlock_wr(&uihash_lock);
714 spin_lock_rd(&uihash_lock);
717 spin_unlock_rd(&uihash_lock);
721 spin_unlock_rd(&uihash_lock);
730 uifree(struct uidinfo *uip)
732 spin_lock_wr(&uihash_lock);
735 * Note that we're taking a read lock even though we
736 * modify the structure because we know nobody can find
737 * it now that we've locked uihash_lock. If somebody
738 * can get to it through a stored pointer, the reference
739 * count will not be 0 and in that case we don't modify
742 spin_lock_rd(&uip->ui_lock);
743 if (uip->ui_ref != 0) {
745 * Someone found the uid and got a ref when we
746 * unlocked. No need to free any more.
748 spin_unlock_rd(&uip->ui_lock);
751 if (uip->ui_sbsize != 0)
752 /* XXX no %qd in kernel. Truncate. */
753 kprintf("freeing uidinfo: uid = %d, sbsize = %ld\n",
754 uip->ui_uid, (long)uip->ui_sbsize);
755 if (uip->ui_proccnt != 0)
756 kprintf("freeing uidinfo: uid = %d, proccnt = %ld\n",
757 uip->ui_uid, uip->ui_proccnt);
759 LIST_REMOVE(uip, ui_hash);
760 spin_unlock_wr(&uihash_lock);
761 varsymset_clean(&uip->ui_varsymset);
762 lockuninit(&uip->ui_varsymset.vx_lock);
763 spin_unlock_rd(&uip->ui_lock);
764 spin_uninit(&uip->ui_lock);
765 FREE(uip, M_UIDINFO);
772 uihold(struct uidinfo *uip)
774 atomic_add_int(&uip->ui_ref, 1);
775 KKASSERT(uip->ui_ref > 0);
782 uidrop(struct uidinfo *uip)
784 if (atomic_fetchadd_int(&uip->ui_ref, -1) == 1) {
787 KKASSERT(uip->ui_ref > 0);
792 uireplace(struct uidinfo **puip, struct uidinfo *nuip)
799 * Change the count associated with number of processes
800 * a given user is using. When 'max' is 0, don't enforce a limit
803 chgproccnt(struct uidinfo *uip, int diff, int max)
806 spin_lock_wr(&uip->ui_lock);
807 /* don't allow them to exceed max, but allow subtraction */
808 if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
811 uip->ui_proccnt += diff;
812 if (uip->ui_proccnt < 0)
813 kprintf("negative proccnt for uid = %d\n", uip->ui_uid);
816 spin_unlock_wr(&uip->ui_lock);
821 * Change the total socket buffer size a user has used.
824 chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max)
828 spin_lock_wr(&uip->ui_lock);
829 new = uip->ui_sbsize + to - *hiwat;
833 * If we are trying to increase the socket buffer size
834 * Scale down the hi water mark when we exceed the user's
835 * allowed socket buffer space.
837 * We can't scale down too much or we will blow up atomic packet
840 if (to > *hiwat && to > MCLBYTES && new > max) {
845 uip->ui_sbsize = new;
847 spin_unlock_wr(&uip->ui_lock);